Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop when trying to copy "recursive" array #166

Open
sebastianbergmann opened this issue Aug 21, 2021 · 2 comments
Open

Infinite loop when trying to copy "recursive" array #166

sebastianbergmann opened this issue Aug 21, 2021 · 2 comments
Labels

Comments

@sebastianbergmann
Copy link

It would be nice if DeepCopy could detect array structures that lead to infinite loops.

$ composer info | sort -d
myclabs/deep-copy 1.10.2 Create deep copies (clones) of your objects
$ php -v                           
PHP 8.0.9 (cli) (built: Jul 29 2021 12:53:58) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.9, Copyright (c) Zend Technologies

segfault.php

<?php declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';

use DeepCopy\DeepCopy;

$a   = [];
$a[] = &$a;

$b = [
    'RECURSION' => [
        'data' => $a,
    ],
];

$deepCopy = new DeepCopy;
$deepCopy->skipUncloneable(false);

$deepCopy->copy($b);

Without Xdebug

$ php segfault.php              
[1]    49178 segmentation fault (core dumped)  php segfault.php

With Xdebug

$ php segfault.php                 

Fatal error: Uncaught Error: Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '256' frames in /tmp/segfault/vendor/myclabs/deep-copy/src/DeepCopy/TypeMatcher/TypeMatcher.php on line 27
@mnapoli
Copy link
Member

mnapoli commented Aug 26, 2021

Thanks for the report.

I gave it a quick try and I honestly don't see how to achieve this.

To detect/avoid recursions in object graphs, we use a hash map of spl_object_hash() -> object.

I don't see how to do a similar hash map with primitive values 🤔

@sebastianbergmann
Copy link
Author

No worries, and feel free to close this report. I ran into this while working on some PHPUnit internals and suddenly a test for a weird edge case that uses such a data structure triggered this crash. I'll investigate this on my end, maybe we can simply delete the test (and document the fact that such weird data structures are not supported).

@mnapoli mnapoli added the bug label Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants