Skip to content

Commit 2210c68

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
2 parents 46807ec + af324e2 commit 2210c68

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
References to objects for which Serializable::serialize() returned NULL should use N;
3+
--FILE--
4+
<?php
5+
6+
class NotSerializable implements Serializable {
7+
public function serialize() {
8+
return null;
9+
}
10+
11+
public function unserialize($serialized) {
12+
}
13+
}
14+
15+
$obj = new NotSerializable();
16+
$data = [$obj, $obj];
17+
var_dump($s = serialize($data));
18+
var_dump(unserialize($s));
19+
20+
?>
21+
--EXPECT--
22+
string(18) "a:2:{i:0;N;i:1;N;}"
23+
array(2) {
24+
[0]=>
25+
NULL
26+
[1]=>
27+
NULL
28+
}

ext/standard/var.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,11 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
857857
}
858858

859859
if (var_hash && (var_already = php_add_var_hash(var_hash, struc))) {
860-
if (Z_ISREF_P(struc)) {
860+
if (var_already == -1) {
861+
/* Reference to an object that failed to serialize, replace with null. */
862+
smart_str_appendl(buf, "N;", 2);
863+
return;
864+
} else if (Z_ISREF_P(struc)) {
861865
smart_str_appendl(buf, "R:", 2);
862866
smart_str_append_long(buf, var_already);
863867
smart_str_appendc(buf, ';');
@@ -921,6 +925,10 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
921925
smart_str_appendl(buf, (char *) serialized_data, serialized_length);
922926
smart_str_appendc(buf, '}');
923927
} else {
928+
/* Mark this value in the var_hash, to avoid creating references to it. */
929+
zval *var_idx = zend_hash_index_find(&var_hash->ht,
930+
(zend_ulong) (zend_uintptr_t) Z_COUNTED_P(struc));
931+
ZVAL_LONG(var_idx, -1);
924932
smart_str_appendl(buf, "N;", 2);
925933
}
926934
if (serialized_data) {

0 commit comments

Comments
 (0)