Skip to content

Commit e63a54c

Browse files
derickrbon
authored andcommitted
Fix phpGH-11416: Crash with DatePeriod when uninitialised objects are passed in
1 parent a4a492c commit e63a54c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ PHP NEWS
1818
- Core:
1919
. Fixed strerror_r detection at configuration time. (Kévin Dunglas)
2020

21+
- Date:
22+
. Fixed bug GH-11416: Crash with DatePeriod when uninitialised objects
23+
are passed in. (Derick)
24+
2125
- DOM:
2226
. Fix DOMEntity field getter bugs. (nielsdos)
2327
. Fix incorrect attribute existence check in DOMElement::setAttributeNodeNS.

ext/date/php_date.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4956,6 +4956,12 @@ PHP_METHOD(DatePeriod, __construct)
49564956
RETURN_THROWS();
49574957
}
49584958
} else {
4959+
/* check initialisation */
4960+
DATE_CHECK_INITIALIZED(Z_PHPDATE_P(start)->time, DateTimeInterface);
4961+
if (end) {
4962+
DATE_CHECK_INITIALIZED(Z_PHPDATE_P(end)->time, DateTimeInterface);
4963+
}
4964+
49594965
/* init */
49604966
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
49614967

ext/date/tests/bug-gh11416.phpt

Lines changed: 28 additions & 0 deletions
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)