php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79657 "yield from" hangs when invalid value encountered
Submitted: 2020-05-31 17:09 UTC Modified: 2020-05-31 18:49 UTC
From: max dot tun at pm dot me Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.4.6 OS: Alpine Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: max dot tun at pm dot me
New email:
PHP Version: OS:

 

 [2020-05-31 17:09 UTC] max dot tun at pm dot me
Description:
------------
Starting from PHP 7.4.6 (I've managed to reproduce on 7.4.7RC1 as well) "yield from" hangs when I try to unwrap generator into function arguments and an invalid value is encountered or an exception is thrown.

In the example provided this won't happen if the "get" function doesn't have arguments.
This also doesn't happen if I unwrap the generator into an array.
Also, works as expected if an error occurs on first iteration.

The script hangs on line #15 and CPU usage and memory consumption increases significantly, looks like there's an infinite loop.

Test script:
---------------
<?php

function throwException(): iterable
{
    throw new Exception();
}

function loop(): iterable
{
    $callbacks = [
        function () {
            yield 'first';
        },
        function () {
            yield from throwException();
        }
    ];

    foreach ($callbacks as $callback) {
        yield from $callback();
    }
}

function get(string $first, int $second): array
{
    return [];
}

get(...loop());


Expected result:
----------------
Either this if I throw an exception:

Fatal error: Uncaught Exception in test.php:5
Stack trace:
#0 test.php(15): throwException()
#1 test.php(20): {closure}()
#2 test.php(29): loop()
#3 {main}
  thrown in test.php on line 5


Or the following if an invalid value is being used to yield from:

Fatal error: Uncaught Error: Can use "yield from" only with arrays and Traversables in test.php:15
Stack trace:
#0 test.php(20): {closure}()
#1 test.php(29): loop()
#2 {main}
  thrown in test.php on line 15


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-31 18:49 UTC] requinix@php.net
-Status: Open +Status: Verified -Package: *General Issues +Package: Scripting Engine problem
 [2020-06-08 09:32 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=df2db7fceaff4f46909f1aa8b31f0a9010631fc9
Log: Fixed bug #79657
 [2020-06-08 09:32 UTC] nikic@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 10:01:29 2024 UTC