Fix out-of-bounds heap write in string encoder#304
Merged
Conversation
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
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
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.
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