Bug #80039 [NEW]: Illegal string offset and Cannot create references to/from string offsets

From: Date: Mon, 31 Aug 2020 09:03:06 +0000
Subject: Bug #80039 [NEW]: Illegal string offset and Cannot create references to/from string offsets
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228808@lists.php.net to get a copy of this message
From:             miloslav dot hula at gmail dot com
Operating system: 
PHP version:      7.4.9
Package:          *General Issues
Bug Type:         Bug
Bug description: Illegal string offset and Cannot create references to/from string offsets

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 bug report at https://bugs.php.net/bug.php?id=80039&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80039&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80039&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80039&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80039&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80039&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80039&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80039&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80039&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80039&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80039&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80039&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80039&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80039&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80039&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80039&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80039&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80039&r=mysqlcfg


Thread (9 messages)

« previous php.bugs (#228808) next »