Bug #77050 [Com]: eval and new anonymous class bug
| From: | a at b dot c dot de | Date: | Tue, 23 Oct 2018 04:29:13 +0000 |
| Subject: | Bug #77050 [Com]: eval and new anonymous class bug | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-217660@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77050&edit=1
ID: 77050
Comment by: a at b dot c dot de
Reported by: admin at yurunsoft dot com
Summary: eval and new anonymous class bug
Status: Open
Type: Bug
Package: *General Issues
Operating System: All
PHP Version: 7.1.23
Block user comment: N
Private report: N
New Comment:
Shorter test case:
<?php
class A{}
class B{}
for($i = 0; $i < 10; ++$i)
{
$c = ['A','B'][rand() % 2];
$t = eval("return new class extends $c{};");
echo get_parent_class($t);
}
?>
It doesn't happen when the loop is unrolled:
<?php
$c = ['A','B'][rand() % 2];
$t = eval("return new class extends $c{};");
echo get_parent_class($t);
$c = ['A','B'][rand() % 2];
$t = eval("return new class extends $c{};");
echo get_parent_class($t);
....
?>
But it does happen if the eval is then wrapped in a function:
<?php
function make($c)
{
return eval("return new class extends $c{};");
}
$c = ['A','B'][rand() % 2];
$t = make($c);
echo get_parent_class($t);
$c = ['A','B'][rand() % 2];
$t = make($c);
echo get_parent_class($t);
....
?>
Two identical make() functions:
<?php
class A{}
class B{}
function make1($c)
{
return eval("return new class extends $c{};");
}
function make2($c)
{
return eval("return new class extends $c{};");
}
echo get_parent_class(make1('A')); // Outputs A as expected
echo get_parent_class(make2('B')); // Outputs B as expected
echo get_parent_class(make1('B')); // Outputs A instead of B
echo get_parent_class(make2('A')); // Outputs B instead of A
?>
Previous Comments:
------------------------------------------------------------------------
[2018-10-23 01:51:06] admin at yurunsoft dot com
Description:
------------
eval and new anonymous class, always return the first result
Test script:
---------------
https://gist.github.com/Yurunsoft/f80b6e234a031cdd320d28bb629947a3
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77050&edit=1