Skip to content

Commit 4833b84

Browse files
committed
Fix GH-11416: Crash with DatePeriod when uninitialised objects are passed in
1 parent d19e4da commit 4833b84

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ PHP NEWS
1111
- Core:
1212
. Fixed strerror_r detection at configuration time. (Kévin Dunglas)
1313

14+
- Date:
15+
. Fixed bug GH-11416: Crash with DatePeriod when uninitialised objects
16+
are passed in. (Derick)
17+
1418
- DOM:
1519
. Fix DOMEntity field getter bugs. (nielsdos)
1620
. Fix incorrect attribute existence check in DOMElement::setAttributeNodeNS.

ext/date/php_date.c

+6
Original file line numberDiff line numberDiff line change
@@ -4338,6 +4338,12 @@ PHP_METHOD(DatePeriod, __construct)
43384338
}
43394339
dpobj->start_ce = date_ce_date;
43404340
} else {
4341+
/* check initialisation */
4342+
DATE_CHECK_INITIALIZED(Z_PHPDATE_P(start)->time, DateTimeInterface);
4343+
if (end) {
4344+
DATE_CHECK_INITIALIZED(Z_PHPDATE_P(end)->time, DateTimeInterface);
4345+
}
4346+
43414347
/* init */
43424348
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
43434349

ext/date/tests/bug-gh11416.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug GH-11416: Crash with DatePeriod when uninitialised objects are passed in
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
$now = new DateTimeImmutable();
8+
9+
$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
10+
try {
11+
new DatePeriod($date, new DateInterval('P1D'), 2);
12+
} catch (Error $e) {
13+
echo get_class($e), ': ', $e->getMessage(), "\n";
14+
}
15+
16+
$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
17+
try {
18+
new DatePeriod($now, new DateInterval('P1D'), $date);
19+
} catch (Error $e) {
20+
echo get_class($e), ': ', $e->getMessage(), "\n";
21+
}
22+
23+
echo "OK\n";
24+
?>
25+
--EXPECT--
26+
Error: The DateTimeInterface object has not been correctly initialized by its constructor
27+
Error: The DateTimeInterface object has not been correctly initialized by its constructor
28+
OK

0 commit comments

Comments
 (0)