Repository navigation
Conversation
VSEncoding::compute_OptPartition() scans the candidate blocks that end at position i, from length 1 upward, and advances l through possLens[] as the lengths match. A block of non-zero integers is at most 16 long, but the scan only stops at that length once i is past maxBlk (32, the longest run of zeros). For positions 17 to 32 it keeps going, l reaches poss_sz, and possLens[l] reads one word past the 16-entry table. Every input of 17 or more integers does this, and AddressSanitizer reports a global-buffer-overflow in the encoder. Check l against poss_sz before reading possLens[l], in the non-zero branch and in the zero-run branch, which has the same unchecked read. When l is in bounds nothing changes. The old code could only pick a different partition if the word after the table held a block length from 17 to 32. Extend the VSEncoding test from 16 to 300 integers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
For every input of 17 or more integers,
VSEncoding::compute_OptPartition()reads__vseblocks_possLens[16], one word past the end of the 16-entry table. Under AddressSanitizer that is a global-buffer-overflow, so a sanitizer build aborts the first time it encodes more than 16 integers with VSEncoding. Without a sanitizer the read is silent, and on the builds I checked the word after the table is 0 or 1, so the output was not affected. That is luck of the memory layout. If that word held a number from 17 to 32, the encoder could choose a block of non-zero integers of that length, which the format has no code for, and the result would not decode.The partition loop tries block lengths 1, 2, 3 and so on for each end position i, and advances
lthroughpossLens[]as they match. It cuts non-zero blocks at 16 only oncei > maxBlk, andmaxBlkis 32, the longest run of zeros, so for positions 17 to 32 it keeps going afterlhas reachedposs_sz.The fix checks
lagainstposs_szbefore readingpossLens[l], in the non-zero branch where the read happens and in the zero-run branch, which has the same read. That branch only runs for a log of 0, which__vseblocks_remapLogs[1 + asmbits(v)]does not produce for values below 2^31, so there the check only matters if that ever changes. Whenlis in bounds the code does exactly what it did before.The test from #150 stopped at 16 integers because of this bug. It now runs its two inputs, increasing values and all zeros, at every length from 0 to 300. That spans many blocks and several descriptor words, so it also covers the whole-word decode path from #150, which 16 integers never reached. Under AddressSanitizer the test fails without the fix at 17 integers with the global-buffer-overflow, and passes with it. Without a sanitizer it passes either way, since the stray read did not change the output on these builds, so the sanitizer CI job is what catches a regression.
Validation on macOS 26.3.1 arm64, with Apple clang 21 unless noted
FASTPFOR_SANITIZE=ON,FASTPFOR_SIMD_MODE=portable, as in the sanitizer CI job. Without the fix the test aborts withAddressSanitizer: global-buffer-overflowinVSEncoding::compute_OptPartitionatvsencoding.h:210, 0 bytes after__vseblocks_possLens. With the fix the gtest binary passes 73 of 73. The VSEncoding test now takes about 1 s here, up from 50 ms, almost all of it in constructing aVSEncodingBlocks, with its 1 MB scratch buffer, for each input.FASTPFOR_SIMD_MODE=native, as in the macOS CI job. 73 of 73 pass, and vsencoding.h gets no new warnings under the library's warning flags.-msse4.2, which GCC rejects on arm64. GCC 16 Release passes too.CODECFactory. Without the fix it hits the same overflow, this time in the copy of the table in codecfactory.cpp, and with the fix it prints "Your code is good." I ran it withASAN_OPTIONS=detect_container_overflow=0, since otherwise it stops earlier at an unrelated read inSimple9_RLEdecoding.Not verified. Linux, the x86 SSE builds, GCC's portable mode, Windows and MSVC. I only looked at what sits after the table on these builds, so another compiler or linker could have put a different word there.
This follows #150 and extends its test.
AI assistance (Claude Code) was used to investigate the issue, write the fix and tests, and run the validation.
🤖 Generated with Claude Code