Skip to content

Make pbf_reader actually provide the strong exception guarantee - #163

Merged
joto merged 1 commit into
masterfrom
kk/reader-strong-exception-guarantee
Oct 15, 2026
Merged

joto merged 1 commit into
masterfrom
kk/reader-strong-exception-guarantee

Conversation

@kkaefer

@kkaefer kkaefer commented Oct 14, 2026

Copy link
Copy Markdown
Member

Our docs promise that a method that throws leaves the reader unchanged. However, this is not true currently because several methods advanced the reader before all checks had passed:

  • next() on tag 0, a reserved tag, or an unknown wire type
  • skip(), get_view(), get_message() on a truncated length-delimited field
  • get_packed_*() on a truncated last varint or a fixed length that isn't a multiple of the element size
  • next(tag) / next(tag, type) when a field after one they've already skipped is invalid

For fixing this, we are now decoding and validating the length on a temporary pointer (peek_length_delimited()), and only move the reader (with consume_until()) after every check has passed. Same for next(), which writes tag, wire type and position only after validating them. next(tag) works on a copy of the reader because it calls many different functions (like skipping) that modify the state as it tries to find the next matching tag.

I also added new unit tests (test_exception_guarantee.cpp) for each case with manually constructed adversary protobuf files, and tested locally with ASan/UBSan. I couldn't find a measurable performance change in the vector-tile decode benchmarks, all the runs were within measurement inaccuracy.

Several methods advanced the reader before all checks had passed, so a
throwing call left the reader in a changed state, contrary to the class
documentation:

- next() on an invalid tag or wire type
- get_view()/get_message()/skip() on a truncated length-delimited field
- get_packed_*() on a truncated last varint or an invalid fixed length
- next(tag) and next(tag, type) when skipping over an invalid field

Length-delimited fields are now decoded and validated without changing
the reader, which is only advanced once every check has passed.
@kkaefer
kkaefer requested a review from a team as a code owner October 14, 2026 09:12
@kkaefer
kkaefer requested review from joto and removed request for a team October 14, 2026 09:12
@kkaefer

kkaefer commented Oct 14, 2026 •

Copy link
Copy Markdown
Member Author

FWIW, I don't think this is a huge problem in practice because if we throw an exception due to malformed file (or badly written reader), I'd expect most client apps would abort the entire parse anyway, so it doesn't really matter that the reader is now in an invalid state.

@joto
joto merged commit 9d98c2b into master Oct 15, 2026
68 checks passed
@kkaefer
kkaefer deleted the kk/reader-strong-exception-guarantee branch October 15, 2026 21:10
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.

2 participants