1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Fix GH-19701: Serialize/deserialize loses some data

See GH-19701 for discussion.
This now restores the (correct) serialization output from versions
PHP 7.4.1 and below.

Closes GH-19762.
This commit is contained in:
Niels Dossche
2025-09-08 18:55:47 +02:00
parent 2ad0b5cf05
commit 4974d5ef49
3 changed files with 32 additions and 8 deletions

1
NEWS
View File

@@ -12,6 +12,7 @@ PHP NEWS
- Standard:
. Fixed bug GH-12265 (Cloning an object breaks serialization recursion).
(nielsdos)
. Fixed bug GH-19701 (Serialize/deserialize loses some data). (nielsdos)
- Zip:
. Fixed bug GH-19688 (Remove pattern overflow in zip addGlob()). (nielsdos)

View File

@@ -0,0 +1,30 @@
--TEST--
GH-19701 (Serialize/deserialize loses some data)
--CREDITS--
cuchac
DanielEScherzer
--FILE--
<?php
class Item {
public $children = [];
public $parent = null;
public function __sleep() {
return ["children", "parent"];
}
}
$baseProduct = new Item();
$child = new Item();
$child->parent = $baseProduct;
$baseProduct->children = [ $child ];
$data = [clone $baseProduct, $baseProduct];
echo serialize($data), "\n";
?>
--EXPECT--
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;}

View File

@@ -954,18 +954,11 @@ static void php_var_serialize_nested_data(smart_str *buf, zval *struc, HashTable
/* we should still add element even if it's not OK,
* since we already wrote the length of the array before */
if (Z_TYPE_P(data) == IS_ARRAY) {
if (UNEXPECTED(Z_IS_RECURSIVE_P(data))
|| UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
if (UNEXPECTED(Z_TYPE_P(struc) == IS_ARRAY && Z_ARR_P(data) == Z_ARR_P(struc))) {
php_add_var_hash(var_hash, struc, in_rcn_array);
smart_str_appendl(buf, "N;", 2);
} else {
if (Z_REFCOUNTED_P(data)) {
Z_PROTECT_RECURSION_P(data);
}
php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);
if (Z_REFCOUNTED_P(data)) {
Z_UNPROTECT_RECURSION_P(data);
}
}
} else {
php_var_serialize_intern(buf, data, var_hash, in_rcn_array, false);