Commit 31a5572f019 for php
commit 31a5572f019034eb5afdf433d6aed966940a5796
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date: Wed Nov 18 23:12:18 2026 +0100
ext/date: Fix truncated timezone name for UTC offsets of 100 hours or more (GH-25457)
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.
Closes GH-25457
diff --git a/NEWS b/NEWS
index 2b369fadce4..01788956353 100644
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,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 d97dc082593..7b8fec3d2da 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 00000000000..4b0cf72c184
--- /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--
+<?php
+var_dump(DateTime::createFromFormat('O', '+9999')->getTimezone()->getName());
+?>
+--EXPECT--
+string(7) "+100:39"