[php-src] PR #25895: Fix GH-24235: DateTime::createFromFormat() segfaults when "!" follows a parsed time zone and the passed zone is an abbreviation
or offset

From: Date: Sun, 29 Nov 2026 10:57:38 +0000
Subject: [php-src] PR #25895: Fix GH-24235: DateTime::createFromFormat() segfaults when "!" follows a parsed time zone and the passed zone is an abbreviation
or offset
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-41185@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/25895 Author: shuhuNB515 Fixes GH-24235 ## Problem ```php <?php date_default_timezone_set('Europe/London'); var_dump(DateTime::createFromFormat('e !Y-m-d H:i', 'Europe/London 2024-10-27 01:30', new DateTimeZone('EST'))); ``` segfaults (exit status 139 / SIGSEGV), while the input is valid and matches the format. ### Root cause The ! specifier calls timelib_time_reset_fields(), which sets tz_info = NULL but leaves the rest of the timezone state (zone_type, z, dst, tz_abbr) from a zone parsed earlier in the format: https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.re#L2143-L2153 Afterwards, timelib_fill_holes() cannot repair the state: - parsed->tz_info is NULL and now->tz_info is also NULL (abbreviation/offset zones don't set it), so tz_info stays NULL; - parsed->zone_type is still TIMELIB_ZONETYPE_ID (non-zero), so the fallback now->zone_type is **not** copied either. The resulting timelib_time has zone_type == TIMELIB_ZONETYPE_ID with tz_info == NULL, and date_format() dereferences it in timelib_fetch_timezone_offset() → segfault. This affects DateTime::createFromFormat(), DateTimeImmutable::createFromFormat() and date_create_from_format(). ## Fix Make timelib_time_reset_fields() reset the whole timezone state, matching the documented semantics of ! ("reset all fields … and timezone to the Unix Epoch"): - zone_type = 0 → timelib_fill_holes() now copies the fallback zone type from now; - z / dst → TIMELIB_UNSET → filled from the fallback zone; - tz_abbr freed and NULLed → filled from the fallback zone; - tz_info = NULL (as before). The same change is applied to both parse_date.re (source) and the checked-in generated parse_date.c (regenerating it would require the re2c 1.0.3 originally used; the edit touches a plain C function outside any re2c-generated block, so the files stay in sync). ## Verification All on a debug build (--enable-debug) of current master: | Step | Result | | --- | --- | | Unpatched build + repro | SIGSEGV (exit 139) at var_dump(), matching the issue | | Patched build + repro | object(DateTime) with timezone_type 2, "EST" — exactly the expected output from the issue | | New test ext/date/tests/bug-gh24235.phpt | passes (covers abbreviation fallback, offset fallback, trailing !, and abbreviation parsed from string) | | make test TESTS="ext/date Zend/tests ext/reflection" | 6867 passed, 0 failed, 0 warnings | Note: ext/date/lib is synced with the upstream timelib repository; the same change will need to be ported to [derickr/timelib](https://github.com/derickr/timelib) (parse_date.re).

« previous php.git-pulls (#41185) next »