Bug #75629 [NEW]: Object property references are replaced by references created in functions
| From: | ocramius@php.net | Date: | Tue, 05 Dec 2017 09:21:04 +0000 |
| Subject: | Bug #75629 [NEW]: Object property references are replaced by references created in functions | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-212931@lists.php.net to get a copy of this message | ||
From: ocramius
Operating system: Linux x64
PHP version: Irrelevant
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Object property references are replaced by references created in functions
Description:
------------
See https://3v4l.org/BNDsu
The PHP engine seems to globally replace references of object properties
when new object property references are created in a by-ref function
call.
The problem occurs when defining a by-ref parameter in a function and
then calling it with an object property:
function foo(& $param) {
// keep a non-gc reference to $param here and things will break
}
The attached test case demonstrates the issue by using
clone and an
array of references. The same example works when the references are kept
in another object.
Possible causes here are:
* clone is misbehaving
* references are misbehaving overall (YIKES!)
* references created in functions are cached somewhere
Test script:
---------------
--TEST--
Keeping a reference to an object property in an array should not
override existing references
?>
--FILE--
<?php
class Container
{
/** @var \stdClass|null */
public $referencedProperty;
public function __clone()
{
$this->referencedProperty = clone $this->referencedProperty;
}
}
$references = [];
$container = new Container();
$loadFieldByReference = function (& $referencedProperty) use (&
$references) {
$referencedProperty = new \stdClass();
$references[] = & $referencedProperty;
};
$loadFieldByReference($container->referencedProperty);
$container->referencedProperty->publicProperty = 123;
$clone = clone $container;
$clone->referencedProperty->publicProperty = 234;
echo $container->referencedProperty->publicProperty, "\n";
echo $clone->referencedProperty->publicProperty, "\n";
echo $container->referencedProperty === $clone->referencedProperty ?
"same\n" : "not same\n";
?>
--EXPECT--
123
234
not same
--
Edit bug report at https://bugs.php.net/bug.php?id=75629&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75629&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75629&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75629&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75629&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75629&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75629&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75629&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75629&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75629&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75629&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75629&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75629&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75629&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75629&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75629&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75629&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75629&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75629&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75629&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75629&r=mysqlcfg