Skip to content

Commit 6e84c41

Browse files
committed
Fix GH-12856: ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties
Closes GH-17590.
1 parent efca8cb commit 6e84c41

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ PHP NEWS
8181
- Reflection:
8282
. Added ReflectionConstant::getExtension() and ::getExtensionName().
8383
(DanielEScherzer)
84+
. Fixed bug GH-12856 (ReflectionClass::getStaticPropertyValue() returns UNDEF
85+
zval for uninitialized typed properties). (nielsdos)
8486

8587
- Session:
8688
. session_start() throws a ValueError on option argument if not a hashmap

ext/reflection/php_reflection.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,16 +4128,21 @@ ZEND_METHOD(ReflectionClass, getStaticPropertyValue)
41284128
prop = zend_std_get_static_property(ce, name, BP_VAR_IS);
41294129
EG(fake_scope) = old_scope;
41304130

4131-
if (prop) {
4131+
if (prop && !Z_ISUNDEF_P(prop)) {
41324132
RETURN_COPY_DEREF(prop);
41334133
}
41344134

41354135
if (def_value) {
41364136
RETURN_COPY(def_value);
41374137
}
41384138

4139-
zend_throw_exception_ex(reflection_exception_ptr, 0,
4140-
"Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4139+
if (prop) {
4140+
zend_throw_error(NULL,
4141+
"Typed property %s::$%s must not be accessed before initialization", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4142+
} else {
4143+
zend_throw_exception_ex(reflection_exception_ptr, 0,
4144+
"Property %s::$%s does not exist", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4145+
}
41414146
}
41424147
/* }}} */
41434148

ext/reflection/tests/gh12856.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-12856 (ReflectionClass::getStaticPropertyValue() returns UNDEF zval for uninitialized typed properties)
3+
--FILE--
4+
<?php
5+
6+
class Bug {
7+
private static $untyped;
8+
private static int $typed1;
9+
private static int $typed2 = 3;
10+
}
11+
12+
$rc = new ReflectionClass(Bug::class);
13+
var_dump($rc->getStaticPropertyValue('untyped'));
14+
try {
15+
var_dump($rc->getStaticPropertyValue('typed1'));
16+
} catch (Error $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
var_dump($rc->getStaticPropertyValue('typed1', 1));
20+
var_dump($rc->getStaticPropertyValue('typed2'));
21+
22+
?>
23+
--EXPECT--
24+
NULL
25+
Typed property Bug::$typed1 must not be accessed before initialization
26+
int(1)
27+
int(3)

0 commit comments

Comments
 (0)