Repository navigation
Zero padding in padded_string_builder::convert() and print padded_string by size - #3156
Merged
Merged
Conversation
…ing by size convert() handed over the uninitialized bytes between size and capacity as the padding of the resulting padded_string, and operator<< printed data() as a C string, so it could write those stale bytes. It also crashed on a null data() and stopped at an embedded NUL. convert() now zeroes the SIMDJSON_PADDING bytes after size, and operator<< writes size() bytes. Reported by @perfloop.
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.
Reported by @perfloop.
padded_string_builderallocates its buffer withallocate_padded_buffer(capacity), which zeroes only the bytes aftercapacity. The bytes betweensizeandcapacityare left uninitialized, andconvert()handed them over as the padding of the resultingpadded_string. As a result:size()could hold stale heap bytes, contrary to thepadded_stringguarantee, and parsing could read uninitialized memory under MSan or valgrind;operator<<printeds.data()as a C string, so it wrote those stale bytes after the content.convert()now zeroes theSIMDJSON_PADDINGbytes aftersize. They are always inside the allocation, which holdscapacity + SIMDJSON_PADDINGbytes.operator<<now writessize()bytes. This also fixes a crash when printing a default-constructed or emptypadded_string(nulldata()), and it no longer stops at an embedded NUL.The new test replaces the array forms of
operator newso that fresh memory holds 0xAA, which makes it fail before the fix regardless of the allocator.