Skip to content

Fix GH-12265: Cloning an object breaks serialization recursion #12287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: PHP-8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ext/standard/tests/serialize/gh12265.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
GH-12265 (Cloning an object breaks serialization recursion) - __serialize variation
--FILE--
<?php

class A {
public function __construct(public B $x) {
}
}

class B {
public A $a;

public function __serialize()
{
return ['a' => new A($this)];
}
}

class C {
public B $b;

public function __construct() {
$this->b = new B;
}
}

$b = new B();
$sb = serialize($b);
$stb = serialize(new B);

printf("serialized original: %s\n", $sb);
printf("serialized temp : %s\n", $stb);

$c = new C;
$sc = serialize($c);
$stc = serialize(new C);

printf("serialized original: %s\n", $sc);
printf("serialized temp : %s\n", $stc);

?>
--EXPECT--
serialized original: O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}
serialized temp : O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}
serialized original: O:1:"C":1:{s:1:"b";O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:2;}}}
serialized temp : O:1:"C":1:{s:1:"b";O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:2;}}}
48 changes: 48 additions & 0 deletions ext/standard/tests/serialize/gh12265b.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
GH-12265 (Cloning an object breaks serialization recursion) - __sleep variation
--FILE--
<?php

class A {
public function __construct(public B $x) {
}
}

class B {
public A $a;

public function __sleep()
{
$this->a = new A($this);
return ['a'];
}
}

class C {
public B $b;

public function __construct() {
$this->b = new B;
}
}

$b = new B();
$sb = serialize($b);
$stb = serialize(new B);

printf("serialized original: %s\n", $sb);
printf("serialized temp : %s\n", $stb);

$c = new C;
$sc = serialize($c);
$stc = serialize(new C);

printf("serialized original: %s\n", $sc);
printf("serialized temp : %s\n", $stc);

?>
--EXPECT--
serialized original: O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}
serialized temp : O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:1;}}
serialized original: O:1:"C":1:{s:1:"b";O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:2;}}}
serialized temp : O:1:"C":1:{s:1:"b";O:1:"B":1:{s:1:"a";O:1:"A":1:{s:1:"x";r:2;}}}
5 changes: 4 additions & 1 deletion ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,10 @@ static inline zend_long php_add_var_hash(php_serialize_data_t data, zval *var, b
return 0;
} else if (!in_rcn_array
&& Z_REFCOUNT_P(var) == 1
&& (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)) {
&& (Z_OBJ_P(var)->properties == NULL || GC_REFCOUNT(Z_OBJ_P(var)->properties) == 1)
/* __serialize and __sleep may arbitrarily increase the refcount */
&& Z_OBJCE_P(var)->__serialize == NULL
&& zend_hash_find_known_hash(&Z_OBJCE_P(var)->function_table, ZSTR_KNOWN(ZEND_STR_SLEEP)) == NULL) {
return 0;
}

Expand Down