Skip to content

Commit 0845f94

Browse files
committed
Add some tests for serialization
1 parent 290c96c commit 0845f94

9 files changed

+129
-4
lines changed

Zend/tests/final_properties/access/property_assignment_combined1.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ try {
7171
echo $exception->getMessage() . "\n";
7272
}
7373

74+
// $property2 must be initialized to null, otherwise the subsequent ??= would do nothing
7475
$foo->property2 ??= null;
7576

7677
try {

Zend/tests/final_properties/access/property_assignment_combined2.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ try {
7171
echo $exception->getMessage() . "\n";
7272
}
7373

74+
// $property2 must be initialized to null, otherwise the subsequent ??= would do nothing
7475
Foo::$property2 ??= null;
7576

7677
try {

Zend/tests/final_properties/access/property_pass_by_ref.phpt renamed to Zend/tests/final_properties/access/property_pass_by_ref1.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test that final properties can't be passed by ref
2+
Test that final properties can't be passed by reference
33
--XFAIL--
44
Incorrect error message in case of static properties ("Cannot modify final static property ...")
55
--FILE--
@@ -42,6 +42,6 @@ try {
4242
?>
4343
--EXPECT--
4444
Cannot acquire reference to final property Foo::$property1
45+
Cannot acquire reference to final property Foo::$property1
46+
Cannot acquire reference to final property Foo::$property2
4547
Cannot acquire reference to final property Foo::$property2
46-
Cannot acquire reference to final property Foo::$property3
47-
Cannot acquire reference to final property Foo::$property4
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Test that final properties can't be passed by reference
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final public array $property1 = ["field" => "abc"];
9+
final public static array $property2 = ["field" => "abc"];
10+
}
11+
12+
$foo = new Foo();
13+
14+
try {
15+
preg_match("/a/", "b", $foo->property1["field"]);
16+
} catch (Error $exception) {
17+
echo $exception->getMessage() . "\n";
18+
}
19+
20+
try {
21+
sort($foo->property1["field"]);
22+
} catch (Error $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
25+
26+
try {
27+
preg_match("/a/", "a", Foo::$property2["field"]);
28+
} catch (Error $exception) {
29+
echo $exception->getMessage() . "\n";
30+
}
31+
32+
try {
33+
sort(Foo::$property2["field"]);
34+
} catch (Error $exception) {
35+
echo $exception->getMessage() . "\n";
36+
}
37+
38+
?>
39+
--EXPECT--
40+
Cannot modify final property Foo::$property1 after initialization
41+
Cannot modify final property Foo::$property1 after initialization
42+
Cannot modify final static property Foo::$property2 after initialization
43+
Cannot modify final static property Foo::$property2 after initialization
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test that final properties can only be assigned to once during unserialization
3+
--XFAIL--
4+
Doesn't work yet.
5+
--FILE--
6+
<?php
7+
8+
class MyClass
9+
{
10+
final public int $finalProp;
11+
}
12+
13+
var_dump(unserialize('O:7:"MyClass":2:{s:9:"finalProp";i:2;s:9:"finalProp";i:3;}'));
14+
15+
?>
16+
--EXPECT--
17+
Cannot modify final property Foo::$finalProp after initialization
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Test that final properties cannot be modified even when they were serialized as non-final
3+
--XFAIL--
4+
Doesn't work yet
5+
--FILE--
6+
<?php
7+
8+
class MyClass
9+
{
10+
final public int $finalProp = 1;
11+
}
12+
13+
$foo = unserialize('O:7:"MyClass":2:{s:9:"finalProp";i:2;s:7:"refProp";R:2;}');
14+
15+
$foo->refProp = 4;
16+
var_dump($foo->finalProp);
17+
18+
?>
19+
--EXPECT--
20+
int(2)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Test that final properties can be serialized and then unserialized
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final public int $property1;
9+
final public string $property2 = "";
10+
}
11+
12+
$foo = new Foo();
13+
14+
var_dump(unserialize(serialize($foo)));
15+
16+
?>
17+
--EXPECT--
18+
object(Foo)#2 (1) {
19+
["property1"]=>
20+
uninitialized(int)
21+
["property2"]=>
22+
string(0) ""
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Test that final properties can be unserialized even when they were serialized as non-final
3+
--FILE--
4+
<?php
5+
6+
class MyClass
7+
{
8+
final public int $finalProp = 1;
9+
}
10+
11+
var_dump(unserialize('O:7:"MyClass":2:{s:9:"finalProp";i:2;s:7:"refProp";R:2;}'));
12+
13+
?>
14+
--EXPECT--
15+
object(MyClass)#1 (2) {
16+
["finalProp"]=>
17+
&int(2)
18+
["refProp"]=>
19+
&int(2)
20+
}

Zend/zend_object_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
819819
variable_ptr = OBJ_PROP(zobj, property_offset);
820820

821821
if (Z_TYPE_P(variable_ptr) != IS_UNDEF) {
822-
if (UNEXPECTED(Z_PROP_FLAG_P(variable_ptr) != IS_PROP_UNINIT && prop_info && prop_info->flags & ZEND_ACC_FINAL)) {
822+
if (UNEXPECTED(prop_info && prop_info->flags & ZEND_ACC_FINAL && Z_PROP_FLAG_P(variable_ptr) != IS_PROP_UNINIT)) {
823823
zend_final_property_assignment_error(zobj->ce, name);
824824
variable_ptr = &EG(error_zval);
825825
goto exit;

0 commit comments

Comments
 (0)