Skip to content

Use checked arithmetic for TZif data block lengths - #1812

Open
n0liu wants to merge 1 commit into
chronotope:mainfrom
n0liu:fix-tzif-count-overflow
Open

Use checked arithmetic for TZif data block lengths#1812
n0liu wants to merge 1 commit into
chronotope:mainfrom
n0liu:fix-tzif-count-overflow

Conversation

@n0liu

@n0liu n0liu commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Addresses the TZif parser finding in #1802.

State::new in the TZif parser computes several data-block lengths by multiplying counts taken straight from the (untrusted) header — transition_count * time_size, type_count * 6, leap_count * (time_size + 4). A malformed header with a huge count overflows the usize multiplication, which panics on 32-bit targets. (On 64-bit the product still fits, and the oversized read_exact fails cleanly, so the panic is 32-bit only.)

Compute those lengths with checked_mul and return Error::InvalidTzFile on overflow, so a malformed header is rejected instead of panicking.

Added a test that feeds a header with type_count = u32::MAX and asserts parsing returns an error. Full test suite, cargo fmt, and clippy -D warnings pass.

The TZif parser computes several data block lengths by multiplying counts
taken straight from the untrusted header (transition_count * time_size,
type_count * 6, leap_count * (time_size + 4)). A malformed header with a
huge count overflows the usize multiplication, which panics on 32-bit
targets. Use checked_mul and return an error instead.

Addresses the TZif parser finding in chronotope#1802.
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.

1 participant