Skip to content

Fall back to level-1 compressor for unsupported compression levels - #255

Open
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix-unsupported-compression-level
Open

Fall back to level-1 compressor for unsupported compression levels#255
rootkiller6788 wants to merge 1 commit into
google:mainfrom
rootkiller6788:fix-unsupported-compression-level

Conversation

@rootkiller6788

Copy link
Copy Markdown

What

CompressionOptions is constructible with any int compression level, and snappy.cc documents levels 3+ as "currently not supported" rather than rejecting them. In InternalCompress, the level dispatch only set end for levels 1 and 2; for any other level end stayed nullptr, and writer->Append(dest, end - dest) then computed a garbage length from undefined behaviour. The string-based Compress() path would throw std::out_of_range from std::string::erase, and the raw path wrote through a wild pointer.

This only manifests with levels outside [1, 2], so the fuzzers (which iterate levels 1..2) never hit it.

Fix

Treat every level other than 2 as the default (level 1) compressor instead of leaving end uninitialized. The existing assert(options.level == 1 || options.level == 2) was relaxed to assert(options.level >= 1) to match the new tolerant dispatch.

Test

Added Snappy.UnsupportedCompressionLevels, which compresses with levels 3..9, asserts the output matches the level-1 baseline, and round-trips through Uncompress. Full suite passes in Debug and Release (25/25).

CompressionOptions accepts any compression level, but InternalCompress only
handles levels 1 and 2. For any other level the `end` pointer was left
nullptr, so `end - dest` was undefined behaviour and the resulting write
length was garbage, which crashed or corrupted the compressed output.

Treat every unsupported level as level 1 instead, and add a regression test
that verifies unsupported levels produce the level-1 output and round-trip
correctly.
@rootkiller6788
rootkiller6788 marked this pull request as ready for review August 26, 2026 15:10
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.

1 participant