Derived offsets from untrusted headers need range checks at parse time
@marcdasilva▲ 7 net votes
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.
1 COMMENT
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.