Skip to content

Reject extended zones whose transitions remain in negative unix time#357

Merged
mkruskal-google merged 9 commits into
google:masterfrom
derekmauro:b529539916
Jul 22, 2026
Merged

Reject extended zones whose transitions remain in negative unix time#357
mkruskal-google merged 9 commits into
google:masterfrom
derekmauro:b529539916

Conversation

@derekmauro

@derekmauro derekmauro commented Jul 21, 2026

Copy link
Copy Markdown
Member

When a TZif file has explicit transitions ending before the epoch and
includes a POSIX DST footer string, ExtendTransitions() sets extended_ = true
and generates 401 years of transitions. Load() previously checked
if (last.unix_time < 0) unconditionally and appended a static sentinel
transition at unix_time = 2147483647 (2038).

Appending a static transition to an extended zone table is incompatible with
the 400-year cycle shifting used for future lookups.

This change:

  • Updates TimeZoneInfo::Load() to reject zoneinfo files if 401 years of
    extended transitions fail to reach the non-negative unix time space
    (if (extended_) return false;). All valid zoneinfo files easily meet
    this requirement.
  • Avoids appending a static no-op transition when extended_ is true,
    preserving the generated 400-year cycle transition table.
  • Guarantees that for any successfully loaded extended zone, last.unix_time
    and last_year_ are non-negative, ensuring that 400-year cycle calculations
    in BreakTime() and MakeTime() operate safely within bounds.
  • Adds unit test coverage in time_zone_lookup_test.cc to verify lookups for
    valid zones with transitions before the epoch, and to confirm rejection of
    zones whose extended transitions fail to reach non-negative unix time.

…zones

When a TZif file has all explicit transitions in the negative-epoch
range (<0) and includes a POSIX DST footer string, ExtendTransitions()
sets extended_ = true and generates 401 years of transitions. Load()
previously checked `if (last.unix_time < 0)` unconditionally and
appended a static sentinel transition at unix_time = 2147483647 (2038).

This corrupted the transition table and lookup behavior:
  1. Lookups for modern dates (e.g., 1970–2038) in BreakTime() skipped
     the `if (extended_)` branch because unix_time < 2147483647, matching
     the negative-epoch static offset and returning standard time (EST)
     instead of daylight saving time (EDT).
  2. Far-future lookups (e.g., time_point<seconds>::max()) caused signed
     integer overflow when computing `shift * kSecsPer400Years` and when
     subtracting negative transition timestamps from large unix times.

This change:
  - Guards the 2038 sentinel addition in TimeZoneInfo::Load() with
    `if (!extended_)`, preserving the extended 400-year cycle transitions.
  - Uses unsigned 64-bit subtraction in TimeZoneInfo::BreakTime() and
    clamps `shift` to `seconds::max().count() / kSecsPer400Years`.
  - Uses unsigned arithmetic in TimeZoneInfo::MakeTime() to safely
    compute civil year differences when `last_year_` is negative.
  - Adds unit test coverage in `time_zone_lookup_test.cc`.
Comment thread src/time_zone_info.cc Outdated
Comment thread src/time_zone_info.cc Outdated
Comment thread src/time_zone_info.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc
Comment thread src/time_zone_lookup_test.cc Outdated
@devbww

devbww commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

PS: It looks like the "This change" part of the PR description could now do with some re-wording.

@derekmauro derekmauro changed the title Fix integer overflow and state corruption in negative-epoch extended zones Reject extended zones whose transitions remain in negative unix time Jul 22, 2026
Comment thread src/time_zone_lookup_test.cc Outdated
Comment thread src/time_zone_lookup_test.cc Outdated
@mkruskal-google
mkruskal-google merged commit be60d62 into google:master Jul 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants