Skip to content

Commit 0a960d5

Browse files
Serialize uninitialized nullable typed properties with a default value to null
1 parent a7f71aa commit 0a960d5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

ext/standard/tests/serialize/sleep_uninitialized_typed_prop.phpt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ Referencing an uninitialized typed property in __sleep() should be skipped
44
<?php
55

66
class Test {
7+
public ?int $w = 123;
78
public int $x;
89
protected int $y;
910
private int $z;
1011

1112
public function __sleep() {
12-
return ['x', 'y', 'z'];
13+
return ['w', 'x', 'y', 'z'];
1314
}
1415

1516
public function __set($name, $val) {
@@ -30,15 +31,23 @@ var_dump(unserialize(serialize($t)) == $t);
3031
$t->z = 3;
3132
var_dump(unserialize(serialize($t)) == $t);
3233

34+
unset($t->w);
35+
$s = serialize($t);
36+
$t->w = null;
37+
var_dump(unserialize($s) == $t);
38+
3339
var_dump($t);
3440
?>
35-
--EXPECT--
36-
string(15) "O:4:"Test":0:{}"
41+
--EXPECTF--
42+
string(29) "O:4:"Test":1:{s:1:"w";i:123;}"
43+
bool(true)
3744
bool(true)
3845
bool(true)
3946
bool(true)
4047
bool(true)
41-
object(Test)#1 (3) {
48+
object(Test)#1 (4) {
49+
["w"]=>
50+
NULL
4251
["x"]=>
4352
int(1)
4453
["y":protected]=>

ext/standard/var.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,20 @@ static int php_var_serialize_try_add_sleep_prop(
782782
if (Z_TYPE_P(val) == IS_INDIRECT) {
783783
val = Z_INDIRECT_P(val);
784784
if (Z_TYPE_P(val) == IS_UNDEF) {
785-
zend_property_info *info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
786-
if (info) {
785+
zend_property_info *prop_info = zend_get_typed_property_info_for_slot(Z_OBJ_P(struc), val);
786+
if (!prop_info) {
787+
return FAILURE;
788+
}
789+
zval *prop;
790+
if ((prop_info->flags & ZEND_ACC_STATIC) != 0) {
791+
prop = &prop_info->ce->default_static_members_table[prop_info->offset];
792+
} else {
793+
prop = &prop_info->ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
794+
}
795+
if (Z_ISUNDEF_P(prop) || !ZEND_TYPE_ALLOW_NULL(prop_info->type)) {
787796
return SUCCESS;
788797
}
789-
return FAILURE;
798+
val = &EG(uninitialized_zval);
790799
}
791800
}
792801

0 commit comments

Comments
 (0)