Repository navigation
Make pbf_reader actually provide the strong exception guarantee - #163
Merged
Merged
Conversation
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.
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
approved these changes
Oct 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 typeskip(),get_view(),get_message()on a truncated length-delimited fieldget_packed_*()on a truncated last varint or a fixed length that isn't a multiple of the element sizenext(tag)/next(tag, type)when a field after one they've already skipped is invalidFor fixing this, we are now decoding and validating the length on a temporary pointer (
peek_length_delimited()), and only move the reader (withconsume_until()) after every check has passed. Same fornext(), 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.