Skip to content

Commit 7989db9

Browse files
committed
Fixed bug #72229 (Wrong reference when serialize/unserialize an object)
1 parent 7e5ea3c commit 7989db9

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PHP NEWS
2121
. Fixed bug #72197 (pg_lo_create arbitrary read). (Anatol)
2222

2323
- Standard:
24+
. Fixed bug #72229 (Wrong reference when serialize/unserialize an object).
25+
(Laruence)
2426
. Fixed bug #72193 (dns_get_record returns array containing elements of
2527
type 'unknown'). (Laruence)
2628
. Fixed bug #72017 (range() with float step produces unexpected result).
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Bug #72229 (Wrong reference when serialize/unserialize an object)
3+
--FILE--
4+
<?php
5+
class C1
6+
{
7+
public $arr1 = array();
8+
public $arr2 = array();
9+
public function __construct()
10+
{
11+
$this->arr1[0] = $this;
12+
$this->arr2[0] = $this->arr1[0];
13+
$var1 = &$this->arr1[0]; // Set a reference...
14+
unset($var1); // ... and unset it.
15+
}
16+
}
17+
$Obj1 = new C1();
18+
$txt1 = serialize($Obj1);
19+
$Obj2 = unserialize($txt1);
20+
$Obj1->arr2[0] = 50;
21+
print_r($Obj1);
22+
$Obj2->arr2[0] = 50;
23+
print_r($Obj2);
24+
?>
25+
--EXPECTF--
26+
C1 Object
27+
(
28+
[arr1] => Array
29+
(
30+
[0] => C1 Object
31+
*RECURSION*
32+
)
33+
34+
[arr2] => Array
35+
(
36+
[0] => 50
37+
)
38+
39+
)
40+
C1 Object
41+
(
42+
[arr1] => Array
43+
(
44+
[0] => C1 Object
45+
*RECURSION*
46+
)
47+
48+
[arr2] => Array
49+
(
50+
[0] => 50
51+
)
52+
53+
)

ext/standard/var.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,10 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
957957
php_var_serialize_string(buf, ZSTR_VAL(key), ZSTR_LEN(key));
958958
}
959959

960+
if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1) {
961+
ZVAL_UNREF(data);
962+
}
963+
960964
/* we should still add element even if it's not OK,
961965
* since we already wrote the length of the array before */
962966
if ((Z_TYPE_P(data) == IS_ARRAY && Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))

0 commit comments

Comments
 (0)