Skip to content

Fixed rare buffer overflow with Encoder::snip()#297

Merged
borrrden merged 1 commit into
masterfrom
fix/snip-overrun-CBL-8363
May 27, 2026
Merged

Fixed rare buffer overflow with Encoder::snip()#297
borrrden merged 1 commit into
masterfrom
fix/snip-overrun-CBL-8363

Conversation

@snej
Copy link
Copy Markdown
Contributor

@snej snej commented May 27, 2026

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 _byte array so that it could be made constexpr (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.

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
@snej snej requested a review from borrrden May 27, 2026 20:34
Copy link
Copy Markdown
Member

@borrrden borrrden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@borrrden borrrden merged commit 4917e4c into master May 27, 2026
5 checks passed
@borrrden borrrden deleted the fix/snip-overrun-CBL-8363 branch May 27, 2026 21:50
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