Skip to content

Commit 358379b

Browse files
committed
Fixed bug #78379 (Cast to object confuses GC, causes crash)
1 parent 954543c commit 358379b

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP NEWS
44

55
- Core:
66
. Fixed bug #78363 (Buffer overflow in zendparse). (Nikita)
7+
. Fixed bug #78379 (Cast to object confuses GC, causes crash). (Dmitry)
78

89
- Curl:
910
. Fixed bug #77946 (Bad cURL resources returned by curl_multi_info_read()).

Zend/tests/bug78379.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Bug #78379 (Cast to object confuses GC, causes crash)
3+
--INI--
4+
opcache.enable=0
5+
--FILE--
6+
<?php
7+
class C {
8+
public function __construct() {
9+
$this->p = (object)["x" => [1]];
10+
}
11+
}
12+
class E {
13+
}
14+
$e = new E;
15+
$e->f = new E;
16+
$e->f->e = $e;
17+
$e->a = new C;
18+
$e = null;
19+
gc_collect_cycles();
20+
var_dump(new C);
21+
?>
22+
--EXPECTF--
23+
object(C)#%d (1) {
24+
["p"]=>
25+
object(stdClass)#%d (1) {
26+
["x"]=>
27+
array(1) {
28+
[0]=>
29+
int(1)
30+
}
31+
}
32+
}

Zend/zend_object_handlers.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ ZEND_API HashTable *zend_std_get_gc(zval *object, zval **table, int *n) /* {{{ *
138138
if (zobj->properties) {
139139
*table = NULL;
140140
*n = 0;
141+
if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)
142+
&& EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
143+
GC_REFCOUNT(zobj->properties)--;
144+
zobj->properties = zend_array_dup(zobj->properties);
145+
}
141146
return zobj->properties;
142147
} else {
143148
*table = zobj->properties_table;

0 commit comments

Comments
 (0)