Commit b5ddbf0f085 for php
commit b5ddbf0f0850f3e00ad6b83b2f1118970e52a87f
Author: hohphu8 <kodoku169@gmail.com>
Date: Sun Nov 29 00:14:13 2026 +0700
ext/intl: Fix crash when formatting an uninitialized DateTime (#25878)
Check that the DateTime object is initialized before reading its
microseconds. Report U_ILLEGAL_ARGUMENT_ERROR when getTimestamp()
returns an integer for an uninitialized object, while preserving the
existing error handling for failures from getTimestamp() itself.
Cover IntlDateFormatter::formatObject(), IntlDateFormatter::format(),
and MessageFormatter::formatMessage() in the regression test.
Closes #25878
diff --git a/NEWS b/NEWS
index 1f6c3bfa1e6..43bcce0c36a 100644
--- a/NEWS
+++ b/NEWS
@@ -120,6 +120,8 @@ PHP NEWS
. Fixed IntlCalendar::setTime() accepting non-finite timestamps. (Weilin Du)
. Fixed MessageFormatter::setPattern() losing the previous pattern when
the new pattern is invalid. (Weilin Du, David Carlier)
+ . Fixed a crash when Intl formatters receive an uninitialized DateTime
+ subclass (GH-25878). (Ho Hoang Phu)
- JSON:
. Fixed json_decode() and json_validate() error messages. (lacatoire)
diff --git a/ext/intl/common/common_date.cpp b/ext/intl/common/common_date.cpp
index a412ddcdffc..a635a4f1881 100644
--- a/ext/intl/common/common_date.cpp
+++ b/ext/intl/common/common_date.cpp
@@ -128,9 +128,17 @@ U_CFUNC int intl_datetime_decompose(zval *z, double *millis, TimeZone **tz,
return FAILURE;
}
+ zval_ptr_dtor(&zfuncname);
datetime = Z_PHPDATE_P(z);
+ if (!datetime->time) {
+ spprintf(&message, 0, "%s: the %s object is not properly "
+ "initialized", func, ZSTR_VAL(Z_OBJCE_P(z)->name));
+ intl_errors_set(err, U_ILLEGAL_ARGUMENT_ERROR,
+ message, 1);
+ efree(message);
+ return FAILURE;
+ }
*millis = U_MILLIS_PER_SECOND * (double)Z_LVAL(retval) + (datetime->time->us / 1000);
- zval_ptr_dtor(&zfuncname);
}
if (tz) {
diff --git a/ext/intl/tests/dateformat_uninitialized_datetime_subclass.phpt b/ext/intl/tests/dateformat_uninitialized_datetime_subclass.phpt
new file mode 100644
index 00000000000..fb0e728e487
--- /dev/null
+++ b/ext/intl/tests/dateformat_uninitialized_datetime_subclass.phpt
@@ -0,0 +1,33 @@
+--TEST--
+intl_datetime_decompose() rejects uninitialized DateTime subclass overriding getTimestamp()
+--EXTENSIONS--
+intl
+--FILE--
+<?php
+
+class UninitDateTime extends DateTime {
+ public function __construct() {}
+ public function getTimestamp(): int {
+ return 0;
+ }
+}
+
+$dt = new UninitDateTime();
+
+var_dump(IntlDateFormatter::formatObject($dt));
+var_dump(intl_get_error_message());
+
+$fmt = new IntlDateFormatter('en_US', IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, 'UTC');
+var_dump($fmt->format($dt));
+var_dump($fmt->getErrorMessage());
+
+var_dump(MessageFormatter::formatMessage('en_US', '{0,date,short}', [$dt]));
+var_dump(intl_get_error_message());
+?>
+--EXPECTF--
+bool(false)
+string(%d) "%sthe UninitDateTime object is not properly initialized: U_ILLEGAL_ARGUMENT_ERROR"
+bool(false)
+string(%d) "%sthe UninitDateTime object is not properly initialized: U_ILLEGAL_ARGUMENT_ERROR"
+bool(false)
+string(83) "The argument for key '0' cannot be used as a date or time: U_ILLEGAL_ARGUMENT_ERROR"