From f14e6786d448f703fe3f884c43b2ba4ee0dd09cc Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Sun, 15 Nov 2026 00:33:29 +0100 Subject: [PATCH] Fix unbounded year loop in DateTimeZone::getTransitions() --- ext/date/php_date.c | 8 ++- ...ateTimeZone_getTransitions_far_future.phpt | 64 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 ext/date/tests/DateTimeZone_getTransitions_far_future.phpt diff --git a/ext/date/php_date.c b/ext/date/php_date.c index d97dc0825937..679b664e100d 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -4458,16 +4458,20 @@ PHP_FUNCTION(timezone_transitions_get) } } if (tzobj->tzi.tz->posix_info && tzobj->tzi.tz->posix_info->dst_end) { - timelib_sll start_y, end_y, dummy_m, dummy_d; + timelib_sll start_y, begin_y, end_y, dummy_m, dummy_d; timelib_sll last_transition_ts = tzobj->tzi.tz->trans[tzobj->tzi.tz->bit64.timecnt - 1]; /* Find out year for last transition */ timelib_unixtime2date(last_transition_ts, &start_y, &dummy_m, &dummy_d); + /* Find out year for first boundary timestamp */ + timelib_unixtime2date(timestamp_begin, &begin_y, &dummy_m, &dummy_d); + /* Find out year for final boundary timestamp */ timelib_unixtime2date(timestamp_end, &end_y, &dummy_m, &dummy_d); - for (timelib_sll i = start_y; i <= end_y; i++) { + /* The previous year's rules can still produce a transition in the range */ + for (timelib_sll i = MAX(start_y, begin_y - 1); i <= end_y; i++) { timelib_posix_transitions transitions = { 0 }; timelib_get_transitions_for_year(tzobj->tzi.tz, i, &transitions); diff --git a/ext/date/tests/DateTimeZone_getTransitions_far_future.phpt b/ext/date/tests/DateTimeZone_getTransitions_far_future.phpt new file mode 100644 index 000000000000..7484a7daf725 --- /dev/null +++ b/ext/date/tests/DateTimeZone_getTransitions_far_future.phpt @@ -0,0 +1,64 @@ +--TEST-- +DateTimeZone::getTransitions() with a far-future range +--SKIPIF-- + +--FILE-- +getTransitions(PHP_INT_MAX - 1000, PHP_INT_MAX))); + +function dump(string $zone, string $begin, string $end): void { + echo "$zone $begin - $end\n"; + $tz = new DateTimeZone($zone); + $b = (new DateTimeImmutable($begin, new DateTimeZone('UTC')))->getTimestamp(); + $e = (new DateTimeImmutable($end, new DateTimeZone('UTC')))->getTimestamp(); + foreach ($tz->getTransitions($b, $e) as $t) { + echo "{$t['ts']} {$t['offset']} ", (int) $t['isdst'], " {$t['abbr']}\n"; + } +} + +dump('Europe/Amsterdam', '2100-01-01', '2110-01-01'); +dump('America/New_York', '2101-01-01', '2103-01-01'); +dump('Australia/Sydney', '2101-01-01', '2103-01-01'); +dump('Australia/Sydney', '2101-01-02', '2101-12-31'); +?> +--EXPECT-- +int(1) +Europe/Amsterdam 2100-01-01 - 2110-01-01 +4102444800 3600 0 CET +4109878800 7200 1 CEST +4128627600 3600 0 CET +4141328400 7200 1 CEST +4160077200 3600 0 CET +4172778000 7200 1 CEST +4191526800 3600 0 CET +4204227600 7200 1 CEST +4222976400 3600 0 CET +4236282000 7200 1 CEST +4254426000 3600 0 CET +4267731600 7200 1 CEST +4285875600 3600 0 CET +4299181200 7200 1 CEST +4317930000 3600 0 CET +4330630800 7200 1 CEST +4349379600 3600 0 CET +4362080400 7200 1 CEST +4380829200 3600 0 CET +4394134800 7200 1 CEST +4412278800 3600 0 CET +America/New_York 2101-01-01 - 2103-01-01 +4133980800 -18000 0 EST +4140140400 -14400 1 EDT +4160700000 -18000 0 EST +4171590000 -14400 1 EDT +4192149600 -18000 0 EST +Australia/Sydney 2101-01-01 - 2103-01-01 +4133980800 39600 1 AEDT +4141900800 36000 0 AEST +4157625600 39600 1 AEDT +4173350400 36000 0 AEST +4189075200 39600 1 AEDT +Australia/Sydney 2101-01-02 - 2101-12-31 +4134067200 39600 1 AEDT +4141900800 36000 0 AEST +4157625600 39600 1 AEDT