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 recursion when recursive / self-referencing arrays are checked whether they contain only scalar values #5567

Closed
ptomulik opened this issue Nov 19, 2023 · 2 comments
Labels
type/bug Something is broken version/10 Something affects PHPUnit 10

Comments

@ptomulik
Copy link
Contributor

Q A
PHPUnit version >= 10.4.0
PHP version 8.2
Installation Method composer

Summary

Current behavior

Tests such as the following end with segmentation fault when run with PHPUnit >= 10.4:

<?php declare(strict_types=1);                                                                                          
                                                                                                                        
use PHPUnit\Framework\TestCase;                                                                                         
                                                                                                                        
final class ExporterTest extends TestCase                                                                               
{                                                                                                                       
    public function testAnythingThatFailsWithRecursiveArray(): void                                                     
    {                                                                                                                   
        $array = [];                                                                                             
        $array['self'] = &$array;                                                                                       
                                                                                                                        
        $this->assertFalse($array);                                                                                     
    }                                                                                                                   
}                                                                                                                       

Here is the run result:

ptomulik@barakus:$ XDEBUG_MODE=coverage vendor/bin/phpunit
PHPUnit 10.4.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: /tmp/phpunit-issue/phpunit.xml

Segmentation fault (core dumped)

With Xdebug enabled I get this:

1) ExporterTest::testAnythingThatFailsWithRecursiveArray
Error: Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '256' frames

How to reproduce

Run a test that is expected to fail and involves a recursive array (see above).

Expected behavior

Shall report failure, as follows (PHPUnit 10.3 does this correctly):

ptomulik@barakus:$ XDEBUG_MODE=coverage vendor/bin/phpunit
PHPUnit 10.3.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.12
Configuration: /tmp/phpunit-issue/phpunit.xml

F                                                                   1 / 1 (100%)

Time: 00:00.012, Memory: 8.00 MB

There was 1 failure:

1) ExporterTest::testAnythingThatFailsWithRecursiveArray
Failed asserting that Array &0 [
    'self' => Array &1 [
        'self' => Array &1,
    ],
] is false.

/tmp/phpunit-issue/tests/ExporterTest.php:12

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Suggestions

There were changes around exporter stuff between PHPUnit 10.3 and 10.4. I guess the root cause of this is the new method PHPUnit\Util\Exporter::isScalarOrArrayOfScalars(). Perhaps it shall use recursion context similarly to all the recursive methods of SebastianBergman\Exporter\Exporter.

@ptomulik ptomulik added type/bug Something is broken version/10 Something affects PHPUnit 10 labels Nov 19, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 19, 2023
@ptomulik
Copy link
Contributor Author

Anyway,

$array = [];                                                                                             
$array['self'] = &$array;

the output for the above array is also wrong:

1) ExporterTest::testAnythingThatFailsWithRecursiveArray
Failed asserting that Array &0 [
    'self' => Array &1 [
        'self' => Array &1,
    ],
] is false.

I'd rather expect just:

1) ExporterTest::testAnythingThatFailsWithRecursiveArray
Failed asserting that Array &0 [
   'self' => Array &0,
] is false.

But this is actually Exporter's issue.

ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 19, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 19, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 19, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 19, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 20, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 20, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 20, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 20, 2023
@ptomulik
Copy link
Contributor Author

Hi,

is it worth to be work on?

I've actually attempted to help with this in #5569, but it was suddenly closed.

ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 30, 2023
ptomulik added a commit to ptomulik/phpunit that referenced this issue Nov 30, 2023
sebastianbergmann pushed a commit to ptomulik/phpunit that referenced this issue Dec 4, 2023
sebastianbergmann pushed a commit to ptomulik/phpunit that referenced this issue Dec 4, 2023
@sebastianbergmann sebastianbergmann changed the title Recursive arrays cause segmentation fault for phpunit >= 10.4 Infinite recursion when recursive / self-referencing arrays are checked whether they contain only scalar values Dec 4, 2023
sebastianbergmann pushed a commit that referenced this issue Dec 4, 2023
sebastianbergmann pushed a commit that referenced this issue Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is broken version/10 Something affects PHPUnit 10
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants