Skip to content

Commit 4974d5e

Browse files
committed
Fix phpGH-19701: Serialize/deserialize loses some data
See phpGH-19701 for discussion. This now restores the (correct) serialization output from versions PHP 7.4.1 and below. Closes phpGH-19762.
1 parent 2ad0b5c commit 4974d5e

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PHP NEWS
1212
- Standard:
1313
. Fixed bug GH-12265 (Cloning an object breaks serialization recursion).
1414
(nielsdos)
15+
. Fixed bug GH-19701 (Serialize/deserialize loses some data). (nielsdos)
1516

1617
- Zip:
1718
. Fixed bug GH-19688 (Remove pattern overflow in zip addGlob()). (nielsdos)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
GH-19701 (Serialize/deserialize loses some data)
3+
--CREDITS--
4+
cuchac
5+
DanielEScherzer
6+
--FILE--
7+
<?php
8+
9+
class Item {
10+
public $children = [];
11+
public $parent = null;
12+
13+
public function __sleep() {
14+
return ["children", "parent"];
15+
}
16+
}
17+
18+
$baseProduct = new Item();
19+
20+
$child = new Item();
21+
$child->parent = $baseProduct;
22+
$baseProduct->children = [ $child ];
23+
24+
$data = [clone $baseProduct, $baseProduct];
25+
26+
echo serialize($data), "\n";
27+
28+
?>
29+
--EXPECT--
30+
a:2:{i:0;O:4:"Item":2:{s:8:"children";a:1:{i:0;O:4:"Item":2:{s:8:"children";a:0:{}s:6:"parent";O:4:"Item":2:{s:8:"children";a:1:{i:0;r:4;}s:6:"parent";N;}}}s:6:"parent";N;}i:1;r:6;}

ext/standard/var.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,11 @@ static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable
954954
/* we should still add element even if it's not OK,
955955
* since we already wrote the length of the array before */
956956
if (Z_TYPE_P(data) == IS_ARRAY) {
957-
if (UNEXPECTED(Z_IS_RECURSIVE_P(data))
958-
|| UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
957+
if (UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
959958
php_add_var_hash(var_hash, struc, in_rcn_array);
960959
smart_str_appendl(buf, "N;", 2);
961960
} else {
962-
if (Z_REFCOUNTED_P(data)) {
963-
Z_PROTECT_RECURSION_P(data);
964-
}
965961
php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
966-
if (Z_REFCOUNTED_P(data)) {
967-
Z_UNPROTECT_RECURSION_P(data);
968-
}
969962
}
970963
} else {
971964
php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);

0 commit comments

Comments
 (0)