Skip to content

Commit c9abfae

Browse files
committed
Fixed bug #78774
The string held by the zend_type may be released if the property type gets resolved to a CE. I initially wanted to fix this by storing a zend_type* instead (so the property type resolution propagates to the ReflectionType), but decided against this in light of upcoming union types support, where we also need to represent parts of the union, and will not have a single zend_type* we can reference.
1 parent e84042c commit c9abfae

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
. Fixed bug #77930 (stream_copy_to_stream should use mmap more often).
1010
(Nikita)
1111

12+
- Reflection:
13+
. Fixed bug #78774 (ReflectionNamedType on Typed Properties Crash). (Nikita)
14+
1215
31 Oct 2019, PHP 7.4.0RC5
1316

1417
- Core:

ext/reflection/php_reflection.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,14 @@ static void reflection_free_objects_storage(zend_object *object) /* {{{ */
224224
efree(intern->ptr);
225225
break;
226226
case REF_TYPE_TYPE:
227-
efree(intern->ptr);
227+
{
228+
type_reference *type_ref = intern->ptr;
229+
if (ZEND_TYPE_IS_NAME(type_ref->type)) {
230+
zend_string_release(ZEND_TYPE_NAME(type_ref->type));
231+
}
232+
efree(type_ref);
228233
break;
234+
}
229235
case REF_TYPE_FUNCTION:
230236
_free_function(intern->ptr);
231237
break;
@@ -1152,6 +1158,12 @@ static void reflection_type_factory(zend_type type, zval *object)
11521158
reference->type = type;
11531159
intern->ptr = reference;
11541160
intern->ref_type = REF_TYPE_TYPE;
1161+
1162+
/* Property types may be resolved during the lifetime of the ReflectionType,
1163+
* so we need to make sure that the strings we reference are not released. */
1164+
if (ZEND_TYPE_IS_NAME(type)) {
1165+
zend_string_addref(ZEND_TYPE_NAME(type));
1166+
}
11551167
}
11561168
/* }}} */
11571169

ext/reflection/tests/bug78774.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #78774: ReflectionNamedType on Typed Properties Crash
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public stdClass $prop;
8+
}
9+
10+
$rc = new ReflectionClass(Test::class);
11+
$rp = $rc->getProperty('prop');
12+
$rt = $rp->getType();
13+
14+
// Force a resolution of the property type
15+
$test = new Test;
16+
$test->prop = new stdClass;
17+
18+
var_dump($rt->getName());
19+
20+
?>
21+
--EXPECT--
22+
string(8) "stdClass"

0 commit comments

Comments
 (0)