Skip to content

Commit ed23ec9

Browse files
committed
Fix memory leak when calling constructor manually
1 parent a5a3723 commit ed23ec9

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

ext/reflection/php_reflection.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,13 +1666,12 @@ ZEND_METHOD(ReflectionFunction, __construct)
16661666
}
16671667
}
16681668

1669-
// TODO How can this ever be hit?
1670-
//zval *object = ZEND_THIS;
1671-
//reflection_object *intern = Z_REFLECTION_P(object);
1672-
//if (intern->ptr) {
1673-
// zval_ptr_dtor(&intern->obj);
1674-
// zval_ptr_dtor(reflection_prop_name(object));
1675-
//}
1669+
/* If the constructor is manually called again we need to free the storage */
1670+
reflection_object *intern = Z_REFLECTION_P(ZEND_THIS);
1671+
if (intern->ptr) {
1672+
zval_ptr_dtor(&intern->obj);
1673+
zval_ptr_dtor(reflection_prop_name(ZEND_THIS));
1674+
}
16761675

16771676
reflection_function_create_common(fptr, closure_obj, ZEND_THIS);
16781677
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
ReflectionFunction constructor called manually after instantiation
3+
--FILE--
4+
<?php
5+
6+
$fn1 = fn ($a) => $a . 'hello';
7+
8+
$r = new ReflectionFunction($fn1);
9+
var_dump($r);
10+
$r->__construct('strpos');
11+
var_dump($r);
12+
13+
?>
14+
--EXPECTF--
15+
object(ReflectionFunction)#2 (1) {
16+
["name"]=>
17+
string(%d) "%s"
18+
}
19+
object(ReflectionFunction)#2 (1) {
20+
["name"]=>
21+
string(6) "strpos"
22+
}

0 commit comments

Comments
 (0)