Skip to content

Commit 72ef37c

Browse files
committed
Serialization fixes
1 parent 8ac9d8e commit 72ef37c

File tree

3 files changed

+51
-36
lines changed

3 files changed

+51
-36
lines changed

ext/date/php_date.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,18 @@ static void date_throw_uninitialized_error(zend_class_entry *ce)
311311
{
312312
if (ce->type == ZEND_INTERNAL_CLASS) {
313313
zend_throw_error(date_ce_date_object_error, "Object of type %s has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
314-
} else {
315-
zend_class_entry *ce_ptr = ce;
316-
while (ce_ptr && ce_ptr->parent && ce_ptr->type == ZEND_USER_CLASS) {
317-
ce_ptr = ce_ptr->parent;
318-
}
319-
if (ce_ptr->type != ZEND_INTERNAL_CLASS) {
320-
zend_throw_error(date_ce_date_object_error, "Object of type %s not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
321-
}
322-
zend_throw_error(date_ce_date_object_error, "Object of type %s (inheriting %s) has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name), ZSTR_VAL(ce_ptr->name));
314+
return;
315+
}
316+
317+
zend_class_entry *ce_ptr = ce;
318+
while (ce_ptr && ce_ptr->parent && ce_ptr->type == ZEND_USER_CLASS) {
319+
ce_ptr = ce_ptr->parent;
320+
}
321+
if (ce_ptr->type != ZEND_INTERNAL_CLASS) {
322+
zend_throw_error(date_ce_date_object_error, "Object of type %s not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name));
323+
return;
323324
}
325+
zend_throw_error(date_ce_date_object_error, "Object of type %s (inheriting %s) has not been correctly initialized by calling parent::__construct() in its constructor", ZSTR_VAL(ce->name), ZSTR_VAL(ce_ptr->name));
324326
}
325327

326328
#define DATE_CHECK_INITIALIZED(member, ce) \
@@ -4046,6 +4048,7 @@ PHP_METHOD(DateTimeZone, __construct)
40464048
if (!timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz), &exception_message)) {
40474049
zend_throw_exception_ex(date_ce_date_invalid_timezone_exception, 0, "DateTimeZone::__construct(): %s", exception_message);
40484050
efree(exception_message);
4051+
RETURN_THROWS();
40494052
}
40504053
}
40514054
/* }}} */
@@ -4620,6 +4623,10 @@ static void php_date_interval_initialize_from_hash(zval **return_value, php_inte
46204623
Z_STRVAL_P(date_str),
46214624
err->error_messages[0].position,
46224625
err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
4626+
4627+
timelib_time_dtor(time);
4628+
timelib_error_container_dtor(err);
4629+
return;
46234630
}
46244631

46254632
(*intobj)->diff = timelib_rel_time_clone(&time->relative);
@@ -5896,13 +5903,14 @@ PHP_METHOD(DatePeriod, __wakeup)
58965903
ZEND_PARSE_PARAMETERS_NONE();
58975904

58985905
period_obj = Z_PHPPERIOD_P(object);
5899-
59005906
myht = Z_OBJPROP_P(object);
59015907

59025908
if (!php_date_period_initialize_from_hash(period_obj, myht)) {
59035909
zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
59045910
RETURN_THROWS();
59055911
}
5912+
5913+
restore_custom_dateperiod_properties(object, myht);
59065914
}
59075915
/* }}} */
59085916

ext/date/tests/date_period_unserialize3.phpt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ $period = new DatePeriod($start, $interval, $end);
1111
try {
1212
$period->__unserialize(
1313
[
14-
"current" => new DateTime,
15-
"start" => new DateTime,
16-
"end" => new DateTime,
14+
"current" => new DateTime("2024-08-27 00:00:00"),
15+
"start" => new DateTime("2024-08-28 00:00:00"),
16+
"end" => new DateTime("2024-08-29 00:00:00"),
1717
"interval" => new DateInterval('P2D'),
1818
"recurrences" => 2,
1919
"include_start_date" => "wrong type",
@@ -33,18 +33,25 @@ object(DatePeriod)#%d (%d) {
3333
["start"]=>
3434
object(DateTime)#%d (%d) {
3535
["date"]=>
36-
string(26) "2022-07-14 00:00:00.000000"
36+
string(26) "2024-08-28 00:00:00.000000"
3737
["timezone_type"]=>
3838
int(3)
3939
["timezone"]=>
4040
string(3) "UTC"
4141
}
4242
["current"]=>
43-
NULL
43+
object(DateTime)#%d (%d) {
44+
["date"]=>
45+
string(26) "2024-08-27 00:00:00.000000"
46+
["timezone_type"]=>
47+
int(3)
48+
["timezone"]=>
49+
string(3) "UTC"
50+
}
4451
["end"]=>
4552
object(DateTime)#%d (%d) {
4653
["date"]=>
47-
string(26) "2022-07-16 00:00:00.000000"
54+
string(26) "2024-08-29 00:00:00.000000"
4855
["timezone_type"]=>
4956
int(3)
5057
["timezone"]=>
@@ -57,7 +64,7 @@ object(DatePeriod)#%d (%d) {
5764
["m"]=>
5865
int(0)
5966
["d"]=>
60-
int(1)
67+
int(2)
6168
["h"]=>
6269
int(0)
6370
["i"]=>
@@ -74,7 +81,7 @@ object(DatePeriod)#%d (%d) {
7481
bool(false)
7582
}
7683
["recurrences"]=>
77-
int(1)
84+
int(2)
7885
["include_start_date"]=>
7986
bool(true)
8087
["include_end_date"]=>

ext/date/tests/gh10747-4.phpt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ $u = unserialize($s);
2626
var_dump($i, str_replace(chr(0), '!', $s), $u);
2727
?>
2828
--EXPECTF--
29-
object(I)#1 (11) {
29+
object(I)#%d (%d) {
30+
["var1":"I":private]=>
31+
int(1)
32+
["var2":"I":private]=>
33+
int(2)
34+
["var3":protected]=>
35+
int(3)
36+
["var4":protected]=>
37+
int(4)
3038
["start"]=>
31-
object(DateTimeImmutable)#5 (3) {
39+
object(DateTimeImmutable)#%d (%d) {
3240
["date"]=>
3341
string(26) "2023-03-03 16:24:00.000000"
3442
["timezone_type"]=>
@@ -39,7 +47,7 @@ object(I)#1 (11) {
3947
["current"]=>
4048
NULL
4149
["end"]=>
42-
object(DateTimeImmutable)#6 (3) {
50+
object(DateTimeImmutable)#%d (%d) {
4351
["date"]=>
4452
string(26) "2023-03-09 16:24:00.000000"
4553
["timezone_type"]=>
@@ -48,7 +56,7 @@ object(I)#1 (11) {
4856
string(3) "UTC"
4957
}
5058
["interval"]=>
51-
object(DateInterval)#7 (10) {
59+
object(DateInterval)#%d (%d) {
5260
["y"]=>
5361
int(0)
5462
["m"]=>
@@ -76,6 +84,9 @@ object(I)#1 (11) {
7684
bool(true)
7785
["include_end_date"]=>
7886
bool(false)
87+
}
88+
string(631) "O:1:"I":11:{s:5:"start";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2023-03-03 16:24:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}s:7:"current";N;s:3:"end";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2023-03-09 16:24:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:0;s:1:"h";i:1;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;s:16:"include_end_date";b:0;s:7:"!I!var1";i:1;s:7:"!I!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}"
89+
object(I)#%d (%d) {
7990
["var1":"I":private]=>
8091
int(1)
8192
["var2":"I":private]=>
@@ -84,11 +95,8 @@ object(I)#1 (11) {
8495
int(3)
8596
["var4":protected]=>
8697
int(4)
87-
}
88-
string(631) "O:1:"I":11:{s:5:"start";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2023-03-03 16:24:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}s:7:"current";N;s:3:"end";O:17:"DateTimeImmutable":3:{s:4:"date";s:26:"2023-03-09 16:24:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}s:8:"interval";O:12:"DateInterval":10:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:0;s:1:"h";i:1;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;}s:11:"recurrences";i:1;s:18:"include_start_date";b:1;s:16:"include_end_date";b:0;s:7:"!I!var1";i:1;s:7:"!I!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}"
89-
object(I)#2 (11) {
9098
["start"]=>
91-
object(DateTimeImmutable)#9 (3) {
99+
object(DateTimeImmutable)#%d (%d) {
92100
["date"]=>
93101
string(26) "2023-03-03 16:24:00.000000"
94102
["timezone_type"]=>
@@ -99,7 +107,7 @@ object(I)#2 (11) {
99107
["current"]=>
100108
NULL
101109
["end"]=>
102-
object(DateTimeImmutable)#10 (3) {
110+
object(DateTimeImmutable)#%d (%d) {
103111
["date"]=>
104112
string(26) "2023-03-09 16:24:00.000000"
105113
["timezone_type"]=>
@@ -108,7 +116,7 @@ object(I)#2 (11) {
108116
string(3) "UTC"
109117
}
110118
["interval"]=>
111-
object(DateInterval)#11 (10) {
119+
object(DateInterval)#%d (%d) {
112120
["y"]=>
113121
int(0)
114122
["m"]=>
@@ -136,12 +144,4 @@ object(I)#2 (11) {
136144
bool(true)
137145
["include_end_date"]=>
138146
bool(false)
139-
["var1":"I":private]=>
140-
int(1)
141-
["var2":"I":private]=>
142-
int(2)
143-
["var3":protected]=>
144-
int(3)
145-
["var4":protected]=>
146-
int(4)
147147
}

0 commit comments

Comments
 (0)