Skip to content

Commit fe504d3

Browse files
committed
Fix leak when creating cycle in hook
This is necessary because the VM frees operands with the nogc variants. We cannot just call gc_possible_root() because the object may no longer exist at that point. Fixes GH-18907 Closes GH-18917
1 parent e3fe9a9 commit fe504d3

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PHP NEWS
88
- Core:
99
. Fixed bug GH-18833 (Use after free with weakmaps dependent on destruction
1010
order). (Daniil Gentili)
11+
. Fixed bug GH-18907 (Leak when creating cycle in hook). (ilutov)
1112

1213
- Curl:
1314
. Fix memory leaks when returning refcounted value from curl callback.

Zend/tests/gh18907.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-18907: Leak when creating cycle inside hook
3+
--FILE--
4+
<?php
5+
6+
class Foo {
7+
public $prop {
8+
get {
9+
$this->prop = $this;
10+
return 1;
11+
}
12+
}
13+
}
14+
15+
function test() {
16+
var_dump((new Foo)->prop);
17+
}
18+
19+
/* Call twice to test the ZEND_IS_PROPERTY_HOOK_SIMPLE_GET() path. */
20+
test();
21+
test();
22+
23+
?>
24+
--EXPECT--
25+
int(1)
26+
int(1)

Zend/zend_object_handlers.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,9 @@ static bool zend_call_get_hook(
719719
return false;
720720
}
721721

722+
GC_ADDREF(zobj);
722723
zend_call_known_instance_method_with_0_params(get, zobj, rv);
724+
OBJ_RELEASE(zobj);
723725

724726
return true;
725727
}

0 commit comments

Comments
 (0)