Skip to content

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

Open
@sebastianbergmann

Description

@sebastianbergmann

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions