Skip to content

Crash when stack walking in destructor of yielded from values during Generator->throw() #14387

Closed

Description

@bwoebi

Description

The following code (https://3v4l.org/QeJLV):

<?php

$gen = (function() {
    yield from [null, new class { function __destruct() { throw new Exception; } }];
})();

function foo($gen) {
    $gen->valid();
}
foo($gen);

function spray() {}
spray(...range(1, 20));

$gen->throw(new Error);

Resulted in segmentation fault.

But I expected this output instead:

Fatal error: Uncaught Exception in /in/QeJLV:4
Stack trace:
#0 /in/QeJLV(4): class@anonymous->__destruct()
#1 [internal function]: {closure}()
#2 /in/QeJLV(15): Generator->throw(Object(Error))
#3 {main}

Next Error in /in/QeJLV:15
Stack trace:
#0 {main}
  thrown in /in/QeJLV on line 15

PHP Version

PHP 8.0.19-8.3.7

Operating System

No response

Activity

  1. self-assigned this
    on May 31, 2024
  2. morrisonlevi commented on May 31, 2024

    @morrisonlevi
    Contributor

    Note that spraying is not always necessary: https://3v4l.org/aqFD6.

    // Primes the generator, sets prev_execute_data to something non-null.
    function prime(Generator $generator)
    {
        $generator->valid();
    }
    
    $g = (function () {
        yield from [null, new class {
            function __destruct()
            {
                // Trigger a stack walk, hitting a bad frame.
                print new Exception;
            }
        }];
    })();
    
    prime($g);
    
    $g->throw(new Error);
  3. added a commit that references this issue on Jun 1, 2024
    be7f3aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions