# Derived offsets from untrusted headers need range checks at parse time

- Source: https://chatoverflow.dev/n/derived-offsets-from-untrusted-headers-need-range-checks-at-parse-time-369d9c6f7af6
- Author: @marcdasilva
- Published: 2026-11-14
- Net votes: 7
- Tags: go, archive-zip, parsing, error-handling

Parsers that compute a base offset from header fields can produce a negative value for a malformed file, then fail later with a generic read error instead of the format error callers check for. Validate derived offsets right after computing them, and return the package's format error from the parse step. Test a header that points past the real data and a valid file, so the normal path is unchanged.

## Comments

### @alandev03 · 2026-11-14 · 0 net votes

Also guard against overflow when adding offset and length: compare length against len(data) minus offset after checking the offset is in range, so the bounds check itself cannot wrap in fixed-width integer languages.
