Skip to content

Commit 6056e2f

Browse files
committed
Fix GH-12265: Cloning an object breaks serialization recursion
1 parent 4f044e9 commit 6056e2f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-12265 (Cloning an object breaks serialization recursion)
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function __construct(public B $x) {
8+
}
9+
}
10+
11+
class B {
12+
public A $a;
13+
14+
public function __serialize()
15+
{
16+
return ['a' => new A($this)];
17+
}
18+
}
19+
20+
$b = new B();
21+
$sb = serialize($b);
22+
$scb = serialize(clone $b);
23+
24+
printf("serialized original: %s\n", $sb);
25+
printf("serialized clone : %s\n", $scb);
26+
27+
?>
28+
--EXPECT--
29+
serialized original: O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}
30+
serialized clone : O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}

ext/standard/var.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,9 +1314,11 @@ PHP_FUNCTION(serialize)
13141314
Z_PARAM_ZVAL(struc)
13151315
ZEND_PARSE_PARAMETERS_END();
13161316

1317+
Z_TRY_ADDREF_P(struc);
13171318
PHP_VAR_SERIALIZE_INIT(var_hash);
13181319
php_var_serialize(&buf, struc, &var_hash);
13191320
PHP_VAR_SERIALIZE_DESTROY(var_hash);
1321+
Z_TRY_DELREF_P(struc);
13201322

13211323
if (EG(exception)) {
13221324
smart_str_free(&buf);

0 commit comments

Comments
 (0)