Skip to content

Commit 41ef059

Browse files
committed
Fix memory leak when calling constructor manually
1 parent eb3c7e8 commit 41ef059

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
@@ -1695,13 +1695,12 @@ ZEND_METHOD(ReflectionFunction, __construct)
16951695
}
16961696
}
16971697

1698-
// TODO How can this ever be hit?
1699-
//zval *object = ZEND_THIS;
1700-
//reflection_object *intern = Z_REFLECTION_P(object);
1701-
//if (intern->ptr) {
1702-
// zval_ptr_dtor(&intern->obj);
1703-
// zval_ptr_dtor(reflection_prop_name(object));
1704-
//}
1698+
/* If the constructor is manually called again we need to free the storage */
1699+
reflection_object *intern = Z_REFLECTION_P(ZEND_THIS);
1700+
if (intern->ptr) {
1701+
zval_ptr_dtor(&intern->obj);
1702+
zval_ptr_dtor(reflection_prop_name(ZEND_THIS));
1703+
}
17051704

17061705
reflection_function_create_common(fptr, closure_obj, ZEND_THIS);
17071706
}
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)