|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77216 recursive generator crash PHP
Submitted: 2018-11-28 21:58 UTC Modified: 2018-11-28 23:25 UTC
From: Assigned:
Status: Duplicate Package: Reproducible crash
PHP Version: 7.3.0RC6 OS: Irrelevant
Private report: No CVE-ID: None
 [2018-11-28 21:58 UTC]
When a generator calls itself (what an idea), PHP crashes. 

Test script:

function generator($n) {
    foreach(generator($n) as $l) {
        yield $l;

foreach(generator(10) as $l) {
    print $l;

// Also

Expected result:
No crash, please.

Can I ask for recursive generators? No? Well, A warning or a nice fail will do. 

Actual result:
Segmentation fault: 11 On OSX

Process exited with code 139. On linux.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-28 22:17 UTC]
-Status: Open +Status: Not a bug
 [2018-11-28 22:17 UTC]
That's just plain infinite recursion.

Recursive generators work fine.
 [2018-11-28 22:31 UTC]
-Status: Not a bug +Status: Re-Opened
 [2018-11-28 22:31 UTC]
@requinix, despite this being an infinite recursion, it should not crash but instead it should throw a memory_limit error, so keeping it open. Similar to other bugs I have reported in the past
 [2018-11-28 23:05 UTC]
This is a stack overflow caused by infinite recursion of
execute_ex().  I doubt that we can catch such issues without
measurable performance impact.  Also, Xdebug has
xdebug.max_nesting_level[1] which should catch such issues during
development.  See also bug #69424.

In my opinion, this is not a bug.

[1] <>
 [2018-11-28 23:07 UTC] spam2 at rhsoft dot net
EVERY uncontrolled crash is a bug - it's that simple
 [2018-11-28 23:08 UTC]
-Status: Re-Opened +Status: Duplicate
 [2018-11-28 23:08 UTC]
Closing as duplicate of bug #64196, which is our canonical recursion stack overflow bug. All of these have the same root cause, but I'm not aware of any work to address this issue.
 [2018-11-28 23:15 UTC]
A bit late but

> it should not crash but instead it should throw a memory_limit error,
Only if PHP runs out of memory. With head recursion the stack might run out before memory does.
 [2018-11-28 23:25 UTC]
@requinix: In most cases, infinite recursion results in a memory limit error, because recursion is performed on the heap-allocated VM stack, rather than the C stack. However, if the recursion involves VM reentry, we do recurse on the C stack, which will ultimately lead to a stack overflow. This usually either happens because an extension that forces VM reentry is used (most profilers fall into this category), or because calls are indirected through internal functions (magic methods, or here, generators).

One "solution" to this problem is to catch the segfault that happens when we hit the stack guard page and display a nice error message in this case. But nobody has tried to implement this yet.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 02 18:01:29 2024 UTC