From 4c001d0ccf3ea2b9c22d9d8387f415d46423bec7 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Fri, 13 Nov 2026 23:04:25 +0100 Subject: [PATCH] Fix truncated timezone name for UTC offsets of 100 hours or more DateTime::createFromFormat() accepts offsets like +9999, whose name does not fit the fixed-size buffer: snprintf() truncated it, and the returned length overran the allocation, so the string was missing its terminating NUL. GH-9763 only rejected such offsets in the DateTimeZone constructor. --- NEWS | 4 ++++ ext/date/php_date.c | 9 +-------- .../tests/createFromFormat_offset_over_99_hours.phpt | 8 ++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 ext/date/tests/createFromFormat_offset_over_99_hours.phpt diff --git a/NEWS b/NEWS index 7b2fd52737b9..65c51fafadc6 100644 --- a/NEWS +++ b/NEWS @@ -30,6 +30,10 @@ PHP NEWS - Curl: . Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire) +- Date: + . Fixed truncated timezone name for UTC offsets of 100 hours or more + created by DateTime::createFromFormat(). (Jorg Sowa) + - DBA: . Fixed dba_close() not closing a database opened with dba_popen(). (Ilia Alshanetsky) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index d97dc0825937..7b8fec3d2da6 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -1940,27 +1940,20 @@ static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, static zend_string *date_create_tz_offset_str(timelib_sll offset) { int seconds = offset % 60; - size_t size; const char *format; if (seconds == 0) { - size = sizeof("+05:00"); format = "%c%02d:%02d"; } else { - size = sizeof("+05:00:01"); format = "%c%02d:%02d:%02d"; } - zend_string *tmpstr = zend_string_alloc(size - 1, 0); - /* Note: if seconds == 0, the seconds argument will be excessive and therefore ignored. */ - ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), size, format, + return zend_strpprintf(0, format, offset < 0 ? '-' : '+', abs((int)(offset / 3600)), abs((int)(offset % 3600) / 60), abs(seconds)); - - return tmpstr; } static void date_object_to_hash(php_date_obj *dateobj, HashTable *props) diff --git a/ext/date/tests/createFromFormat_offset_over_99_hours.phpt b/ext/date/tests/createFromFormat_offset_over_99_hours.phpt new file mode 100644 index 000000000000..4b0cf72c184c --- /dev/null +++ b/ext/date/tests/createFromFormat_offset_over_99_hours.phpt @@ -0,0 +1,8 @@ +--TEST-- +DateTime::createFromFormat() with UTC offset of 100 hours or more +--FILE-- +getTimezone()->getName()); +?> +--EXPECT-- +string(7) "+100:39"