Skip to content

Commit

Permalink
Fix GH-11416: Crash with DatePeriod when uninitialised objects are pa…
Browse files Browse the repository at this point in the history
…ssed in (PHP 8.2+)
  • Loading branch information
derickr committed Aug 9, 2023
1 parent 7f6e98c commit b71d2e1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -8,6 +8,10 @@ PHP NEWS
. Fixed bug GH-10964 (Improve man page about the built-in server).
(Alexandre Daubois)

- Date:
. Fixed bug GH-11416 (Crash with DatePeriod when uninitialised objects are
passed in). (Derick)

- Core:
. Fixed strerror_r detection at configuration time. (Kévin Dunglas)

Expand Down
20 changes: 20 additions & 0 deletions ext/date/php_date.c
Expand Up @@ -5312,6 +5312,11 @@ static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, Has
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
php_date_obj *date_obj;
date_obj = Z_PHPDATE_P(ht_entry);

if (!date_obj->time) {
return 0;
}

if (period_obj->start != NULL) {
timelib_time_dtor(period_obj->start);
}
Expand All @@ -5329,6 +5334,11 @@ static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, Has
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
php_date_obj *date_obj;
date_obj = Z_PHPDATE_P(ht_entry);

if (!date_obj->time) {
return 0;
}

if (period_obj->end != NULL) {
timelib_time_dtor(period_obj->end);
}
Expand All @@ -5345,6 +5355,11 @@ static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, Has
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
php_date_obj *date_obj;
date_obj = Z_PHPDATE_P(ht_entry);

if (!date_obj->time) {
return 0;
}

if (period_obj->current != NULL) {
timelib_time_dtor(period_obj->current);
}
Expand All @@ -5361,6 +5376,11 @@ static bool php_date_period_initialize_from_hash(php_period_obj *period_obj, Has
if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
php_interval_obj *interval_obj;
interval_obj = Z_PHPINTERVAL_P(ht_entry);

if (!interval_obj->initialized) {
return 0;
}

if (period_obj->interval != NULL) {
timelib_rel_time_dtor(period_obj->interval);
}
Expand Down
42 changes: 42 additions & 0 deletions ext/date/tests/bug-gh11416.phpt
Expand Up @@ -5,6 +5,7 @@ date.timezone=UTC
--FILE--
<?php
$now = new DateTimeImmutable();
$simpleInterval = new DateInterval("P2D");

$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
try {
Expand All @@ -20,9 +21,50 @@ try {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

$date = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();
$dateperiod = (new ReflectionClass(DatePeriod::class))->newInstanceWithoutConstructor();
$dateinterval = (new ReflectionClass(DateInterval::class))->newInstanceWithoutConstructor();
try {
$dateperiod->__unserialize(['start' => $date]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

try {
$dateperiod->__unserialize(['start' => $now, 'end' => $date]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

try {
$dateperiod->__unserialize(['start' => $now, 'end' => $now, 'current' => $date]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

try {
$dateperiod->__unserialize(['start' => $now, 'end' => $now, 'current' => $now, 'interval' => $dateinterval]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

try {
$dateperiod->__unserialize([
'start' => $now, 'end' => $now, 'current' => $now, 'interval' => $simpleInterval,
'recurrences' => 2, 'include_start_date' => true, 'include_end_date' => true,
]);
echo "DatePeriod::__unserialize: SUCCESS\n";
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "OK\n";
?>
--EXPECT--
Error: The DateTimeInterface object has not been correctly initialized by its constructor
Error: The DateTimeInterface object has not been correctly initialized by its constructor
Error: Invalid serialization data for DatePeriod object
Error: Invalid serialization data for DatePeriod object
Error: Invalid serialization data for DatePeriod object
Error: Invalid serialization data for DatePeriod object
DatePeriod::__unserialize: SUCCESS
OK

0 comments on commit b71d2e1

Please sign in to comment.