Repository navigation
Conversation
Opening a submessage with a known size (e.g. packed_field_fixed32 with a size argument) stored a sentinel instead of the rollback position, so rollback() failed an assert in debug builds and threw std::length_error in release builds, leaving a partially written field in the buffer. Keep the real rollback position in m_rollback_pos and mark submessages with a known size with a flag bit instead. For these submessages, m_pos now holds the position where the data must end, so closing them can assert that exactly the promised number of bytes was written. The writer doesn't get any bigger.
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.
When a submessage is opened with a known size (e.g.
packed_field_fixed32 field{pw, 1, 2}),open_submessage()stored thesize_is_knownsentinel inm_rollback_posinstead of the actual rollback position. This means that callingrollback()on such a submessage failed an assert in debug builds. In release builds, it calledresize(SIZE_MAX)on the buffer, which threwstd::length_errorand left a partially written field behind. Neither therollback()docs nor the tutorial mention this limitation.Additionally, we didn't check that exactly
sizebytes were written to such a submessage. Writing too few or too many bytes silently produced a broken message, even in debug builds.This PR contains the following changes to address the issues:
m_rollback_posnow always holds the actual rollback position. Submessages with a known size are marked by setting its top bit (known_size_flag). I think we can safely do that because buffer positions never get that large.m_posnow holds the position where the data must end, andclose_submessage()asserts that the promised number of bytes was written.