Fixed rare buffer overflow with Encoder::snip()#297
Merged
Conversation
Encoder::snip() allocates 2 bytes for a Value, but the Value constructor actually initializes all 4 bytes. If the allocation is at the very end of a heap block it'll overwrite the end. In the case of CBSE-22711 the buffer is the Writer's internal buffer so the overwrite clobbered some following state of the Encoder, causing it to crash on the next use. Fixes CBL-8363
borrrden
approved these changes
May 27, 2026
Member
borrrden
left a comment
There was a problem hiding this comment.
This test is dependent on the current layout of the Encoder as its written, but address sanitizer will fail on this test as well so it's not as important as it seems.
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.
Encoder::snip() allocates 2 bytes for a Value, but the Value constructor actually initializes all 4 bytes. If the allocation is at the very end of a heap block it'll overwrite the end.
In the case of CBSE-22711 the buffer is the Writer's internal buffer so the overwrite clobbered some following state of the Encoder, causing it to crash on the next use.
Fixes CBL-8363
Notes on the fix
I changed the Value constructor to only write to the first 2 bytes (i.e. a 'narrow' Value) which was its original intended behavior.
IIRC, the constructor originally did this but I altered it at some point to initialize the entire 4-item
_bytearray so that it could be madeconstexpr(otherwise the compiler complains that the object contains uninitialized data.) We still need the constexpr constructor, but I've differentiated its signature by adding parameters for the other 2 byte values.