php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77050 eval and new anonymous class bug
Submitted: 2018-10-23 01:51 UTC Modified: 2018-10-23 09:21 UTC
From: admin at yurunsoft dot com Assigned: cmb (profile)
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.1.23 OS: All
Private report: No CVE-ID: None
 [2018-10-23 01:51 UTC] admin at yurunsoft dot com
Description:
------------
eval and new anonymous class, always return the first result

Test script:
---------------
https://gist.github.com/Yurunsoft/f80b6e234a031cdd320d28bb629947a3


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-23 04:29 UTC] a at b dot c dot de
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

?>
 [2018-10-23 09:21 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Package: *General Issues +Package: Scripting Engine problem -Assigned To: +Assigned To: cmb
 [2018-10-23 09:21 UTC] cmb@php.net
Duplicate of bug #73816.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 22:01:30 2024 UTC