Bug #80039 [Opn->Csd]: Illegal string offset and Cannot create references to/from string offsets

From: Date: Mon, 31 Aug 2020 09:43:45 +0000
Subject: Bug #80039 [Opn->Csd]: Illegal string offset and Cannot create references to/from string offsets
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228813@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80039&edit=1

 ID:                 80039
 Updated by:         nikic@php.net
 Reported by:        miloslav dot hula at gmail dot com
 Summary:             Illegal string offset and Cannot create references
                     to/from string offsets
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            *General Issues
 PHP Version:        7.4.9
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

I've added the additional test case in http://git.php.net/?p=php-src.git;a=commit;h=8f7c52972089ad7ca6349a036a9cef6da5c4146e.


Previous Comments:
------------------------------------------------------------------------
[2020-08-31 09:21:07] nikic@php.net

Looks like this has been fixed by https://github.com/php/php-src/commit/247105ae1ae2a04608078f7fcfe88dacab9f55a4.

------------------------------------------------------------------------
[2020-08-31 09:19:34] miloslav dot hula at gmail dot com

And when you

$prop = &$cache[$class][$name];
var_dump($prop);

the $prop is NULL?

------------------------------------------------------------------------
[2020-08-31 09:12:48] nikic@php.net

Hm, I can't reproduce this on current 7.4/master HEAD, just get empty output. Possibly this has
been fixed recently?

------------------------------------------------------------------------
[2020-08-31 09:03:06] miloslav dot hula at gmail dot com

Description:
------------
This is probably related to https://bugs.php.net/bug.php?id=80037

The test scripts emits warning and fatal error when creating a reference to static variable.

The test script is reduced to show the error. In production code, there is a relation between
referenced value in fromArray() and $prop = &$cache ...

For example, when I call:

A::fromArray(['a' => 'foo']);

the error is emmited, the exception handler executed and

$prop = &$cache[$class][$name];

normally pass. But, the $prop contains 'foo' string.

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

final class A
{
	public string $a;

	public static function fromArray(array $props): self
	{
		$me = new static;
		foreach ($props as $k => &$v) {
			$me->{$k} = &$v;
		}
		return $me;
	}

	public function __get($name)
	{
		throw new \LogicException("Property '$name' is not defined.");
	}
}

class ObjectHelpers
{
	public static function hasProperty(string $class, string $name)
	{
		static $cache = [];
		$prop = &$cache[$class][$name];  # <-- emits error
	}
}

set_exception_handler(function ($e) {
	ObjectHelpers::hasProperty(A::class, 'a');
});

A::fromArray(['a' => 'foo']);

Expected result:
----------------
(no error)

Actual result:
--------------
Warning: Illegal string offset 'a' in bug.php on line 27

Fatal error:  Uncaught Error: Cannot create references to/from string offsets in bug.php:27
Stack trace:
#0 bug.php(32): ObjectHelpers::hasProperty('A', 'a')
#1 [internal function]: {closure}(Object(Error))
#2 {main}
  thrown in bug.php on line 27



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80039&edit=1


Thread (9 messages)

« previous php.bugs (#228813) next »