Skip to content

Commit f5b9362

Browse files
committed
Fix unserialization ref source management, again
Handle one case the previous patch did not account for: If unserialization of data fails, we should still register a ref source. Also add an extra test for a reference between two typed properties, as this used to be handled incorrectly earlier.
1 parent 518eb0c commit f5b9362

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

ext/standard/tests/serialize/typed_property_ref_assignment_failure.phpt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ try {
1616
echo $e->getMessage(), "\n";
1717
}
1818

19+
$s = <<<'STR'
20+
O:4:"Test":1:{s:4:"prop";a:1:{i:0;R:2;
21+
STR;
22+
var_dump(unserialize($s));
23+
1924
?>
20-
--EXPECT--
25+
--EXPECTF--
2126
Cannot assign stdClass to property Test::$prop of type int
27+
28+
Notice: unserialize(): Error at offset 38 of 38 bytes in %s on line %d
29+
bool(false)

ext/standard/tests/serialize/typed_property_refs.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class B {
1313
public int $b;
1414
}
1515

16+
class E {
17+
public $a;
18+
public int $b;
19+
}
20+
1621
class C {
1722
public int $a;
1823
public string $b;
@@ -25,6 +30,7 @@ class D {
2530

2631
var_dump(unserialize('O:1:"A":2:{s:1:"a";i:1;s:1:"b";R:2;}'));
2732
var_dump(unserialize('O:1:"B":2:{s:1:"a";i:1;s:1:"b";R:2;}'));
33+
var_dump(unserialize('O:1:"E":2:{s:1:"a";i:1;s:1:"b";R:2;}'));
2834

2935
try {
3036
var_dump(unserialize('O:1:"A":2:{s:1:"a";N;s:1:"b";R:2;}'));
@@ -66,6 +72,12 @@ object(B)#1 (2) {
6672
["b"]=>
6773
&int(1)
6874
}
75+
object(E)#1 (2) {
76+
["a"]=>
77+
&int(1)
78+
["b"]=>
79+
&int(1)
80+
}
6981
Cannot assign null to property A::$a of type int
7082
Cannot assign null to property B::$b of type int
7183
Cannot assign int to property C::$b of type string

ext/standard/var_unserializer.re

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ string_key:
578578
}
579579

580580
if (!php_var_unserialize_internal(data, p, max, var_hash, 0)) {
581+
if (info && Z_ISREF_P(data)) {
582+
/* Add type source even if we failed to unserialize.
583+
* The data is still stored in the property. */
584+
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(data), info);
585+
}
581586
zval_ptr_dtor(&key);
582587
goto failure;
583588
}

0 commit comments

Comments
 (0)