Skip to content

Fix out-of-bounds heap write in string encoder#304

Merged
nickva merged 1 commit into
masterfrom
enc-segfault-and-proper
Jul 1, 2026
Merged

Fix out-of-bounds heap write in string encoder#304
nickva merged 1 commit into
masterfrom
enc-segfault-and-proper

Conversation

@nickva

@nickva nickva commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Previously if we had a run of unescaped bytes copid as a single memcpy after reserving space with enc_ensure(run), enc_ensure() could report success without actually growing the buffer so the memcpy would have overwritten memory. There is a good chance it fell around the memory allocate heap metadata so the crash might have shown up the allocator calls in OTP.

This would be pretty hard to hit, it would be something like long strings starting with lots escapes (to push e->i high enough) then also followed by long runs of non-escapes. That explains why property tests never caught this.

The fix is to check the buffer size if he have a buffer already before returning. If it's too small, return otherwise release and allocate one of the right size.

To ensure we catch these kind of errors in the future, enhance property tests by switching to PropEr (code borrowed from Apache CouchDB). For belt-and-suspenders and also added some determistic tests as well for the issue.

Fix #303

Previously if we had a run of unescaped bytes copid as a single memcpy after
reserving space with enc_ensure(run), enc_ensure() could report success without
actually growing the buffer so the memcpy would have overwritten memory. There
is a good chance it fell around the memory allocate heap metadata so the crash
might have shown up the allocator calls in OTP.

This would be pretty hard to hit, it would be something like long strings
starting with lots escapes (to push e->i high enough) then also followed by
long runs of non-escapes. That explains why property tests never caught this.

The fix is to check the buffer size if he have a buffer already before
returning. If it's too small, return otherwise release and allocate one of the
right size.

To ensure we catch these kind of errors in the future, enhance property tests
by switching to PropEr (code borrowed from Apache CouchDB). For
belt-and-suspenders and also added some determistic tests as well for the
issue.

Fix #303
@nickva nickva merged commit 75f3577 into master Jul 1, 2026
24 checks passed
@nickva nickva deleted the enc-segfault-and-proper branch July 1, 2026 00:14
pull Bot pushed a commit to edisplay/couchdb that referenced this pull request Jul 1, 2026
* Reduce encoder memory allocations davisp/jiffy#301
* Encode maps directly without an intermediate EJSON proplist  davisp/jiffy#302
* Fix out-of-bounds heap write in string encoder davisp/jiffy#304
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.

segmentation fault on certain input in 2.0.0

1 participant