Repository navigation
Fix VSEncoding writing past the end of the output buffer - #150
Merged
Merged
Conversation
VSEncodingBlocks::decodeVS() reads four block descriptors per 32-bit word. It always copies 16 integers for a block of non-zero integers and clears 32 for a run of zeros, and it decodes every descriptor of the last word, including the padding ones. Decoding can therefore write up to 34 integers past out + len, or 35 for an empty input, so decoding into a buffer of exactly nvalue integers corrupts the heap. Keep the fast path while a whole descriptor word fits before the end, then decode the remaining blocks one at a time with their exact lengths. For an empty input this also stops reading a descriptor word that does not exist. Fixes fast-pack#128. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #128.
VSEncodingBlocks::decodeVS()reads four block descriptors per 32-bit word. It copies 16 integers for every block of non-zero integers (__vseblocks_copy16) and clears 32 for every run of zeros (__vseblocks_zero32), whatever the block length, and it decodes all four descriptors of the last word, including the padding ones. So decoding writes up to 34 integers pastout + nvalue, or 35 for an empty input. codecs.h only asks callers fornvalueintegers of output, so a caller that follows that, like the Rust wrapper in the issue with 10 integers for 5 values, corrupts the heap. The existing tests never hit it because src/unit.cpp gives every codec 1024 integers of headroom.The fix leaves the fast loop as it is but only runs it while a whole descriptor word, at most 4 * 32 integers, fits before the end. The blocks after that, fewer than 128 integers per call, are decoded one at a time with their exact lengths. For an empty input the old do/while also read a descriptor word past the encoded data and returned a pointer one past the end. That goes away too.
The new test unittest/test_vsencoding.cpp decodes into a buffer with guard words after the
nvalueintegers and checks that they are untouched and that the returned pointer is the end of the encoded data. It runs {1, 2, 3, 4, 5} plus increasing and all-zero inputs of length 0 to 16, and it fails without the fix in every build type, not only under ASan. The inputs stop at 16 on purpose. From 17 integers on,VSEncoding::compute_OptPartitionreads__vseblocks_possLens[16], one past the end of the table, and ASan aborts in the encoder. That is a separate bug and this PR does not touch it.Validation on macOS 26.3.1 arm64 with Apple clang 21
FASTPFOR_SANITIZE=ON,FASTPFOR_SIMD_MODE=portable, as in the sanitizer CI job. Without the fix the new test fails, every length from 0 to 16 writes 33 to 35 integers past the buffer. A standalone program that decodes {1, 2, 3, 4, 5} into astd::vector<uint32_t>(5)getsheap-buffer-overflow ... WRITE of size 64in__vseblocks_copy16called fromdecodeVS, the same corruption as in the issue. With the fixctestpasses, the gtest binary passes 73 of 73, and the standalone program decodes cleanly.FASTPFOR_SIMD_MODE=native, as in the macOS CI job. 73 of 73 pass with the fix, and the Release build without ASan fails the new test without the fix.Not verified. GCC, Linux, Windows and MSVC, and the x86 inline asm versions of copy16 and zero32. Also not fixed here, the decoder still reads past the end of a tightly sized input buffer (the unpackers work in groups of 32 integers), and integers of 2^31 and above still do not round trip because
__vseblocks_remapLogs[1 + asmbits(v)]reads past its 33 entries.About CI. Since #148, building all targets fails on Linux and macOS with
ztimer.h:122: extraneous closing brace, so Ubuntu-CI and Macos-CI are red on master and will be red here too until #149 is merged. For the runs above I built the FastPFOR_unittest target directly.AI assistance (Claude Code) was used to investigate the issue, write the fix and tests, and run the validation.
🤖 Generated with Claude Code