Skip to content

Commit 8973390

Browse files
committed
fix bug #64146 (serialize incorrectly saving objects when they are
cloned)
1 parent e8ae795 commit 8973390

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
Bug #64146 (serialize incorrectly saving objects when they are cloned)
3+
--FILE--
4+
<?php
5+
6+
echo "Test\n";
7+
8+
class A
9+
{
10+
public $a = array();
11+
12+
public function __construct()
13+
{
14+
$this->a[] = new B(1);
15+
$this->a[] = new B(2);
16+
}
17+
}
18+
19+
class B implements Serializable
20+
{
21+
public $b;
22+
23+
public function __construct($c)
24+
{
25+
$this->b = new C($c);
26+
}
27+
28+
public function serialize()
29+
{
30+
return serialize(clone $this->b);
31+
}
32+
33+
public function unserialize($data)
34+
{
35+
$this->b = unserialize($data);
36+
}
37+
}
38+
39+
class C
40+
{
41+
public $c;
42+
43+
public function __construct($c)
44+
{
45+
$this->c = $c;
46+
}
47+
}
48+
49+
$a = unserialize(serialize(new A()));
50+
51+
print $a->a[0]->b->c . "\n";
52+
print $a->a[1]->b->c . "\n";
53+
54+
?>
55+
Done
56+
--EXPECT--
57+
Test
58+
1
59+
2
60+
Done

ext/standard/var.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,9 @@ static inline int php_add_var_hash(HashTable *var_hash, zval *var, void *var_old
549549
char id[32], *p;
550550
register int len;
551551

552-
/* relies on "(long)" being a perfect hash function for data pointers,
553-
* however the actual identity of an object has had to be determined
554-
* by its object handle since 5.0. */
555552
if ((Z_TYPE_P(var) == IS_OBJECT) && Z_OBJ_HT_P(var)->get_class_entry) {
556-
p = smart_str_print_long(id + sizeof(id) - 1, (long) Z_OBJ_HANDLE_P(var));
553+
p = smart_str_print_long(id + sizeof(id) - 1,
554+
(long) zend_objects_get_address(var TSRMLS_CC));
557555
*(--p) = 'O';
558556
len = id + sizeof(id) - 1 - p;
559557
} else {

0 commit comments

Comments
 (0)