Fix literal overrun validation in Snappy decompression
Summary
Fixes #152. Snappy.Decompress and the other block APIs accepted corrupt blocks whose literals run past the declared uncompressed length, returning plausible-looking data instead of throwing InvalidDataException. Google's snappy rejects these blocks.
This PR also includes the 32-bit long literal overflow fix from #155, so both are fixed at once. That one is a memory-safety issue: on x86, a corrupt 4-byte literal length could become negative, get past the bounds check in Append, and start a ~2 GB copy.
Root cause
Five gaps in literal length handling:
- Output bounds used the buffer capacity, not the declared length. The lookback buffer is rented from the
ArrayPoolor supplied by anIBufferWriter, so it is often larger than the declared uncompressed length (for example, 16 bytes for a 10-byte block). A literal could write past the declared length and still pass the bounds check. - A literal split across input was only checked for the bytes present. When a literal extends past the end of the current input, the decompressor appends what is available and expects the rest in a later call. The full declared literal length was never checked against the remaining output. For the block in #152 (an 18-byte literal with 10 bytes of input and a declared length of 10), it wrote 10 bytes, reached the declared length, and reported success.
- A 4-byte literal length could wrap when a tag was resumed from scratch. That path decoded the length as a
uint, so a length of0xFFFFFFFFplus one wrapped to 0. The literal was treated as empty and decoding continued with the following bytes as tags. (Found by Copilot review.) Helpers.ExtractLowBytesreturned 0 for 4 bytes without BMI2. The fallback built its mask with0xffffffff << 32, which C# treats as a shift by 0. On hardware without BMI2 (including ARM and .NET Framework), a 4-byte literal length decoded as 1. That also broke valid literals over 16 MiB on those hosts. (Found by CI on the BMI2-disabled and ARM legs.)- A 4-byte literal length could overflow
ninton x86.nintis 32-bit there, so a length of0x7FFFFFFFor more became negative or wrapped to zero after the + 1. A negative length got past the bounds check inAppend, and the cast touintwould copy ~2 GB. (From #155.)
Changes
- Bound all writes in
DecompressAllTagsandAppendbyExpectedLengthinstead of the lookback buffer's capacity. - Add
EnsureLiteralFits, called before accepting a partial literal. It checks the full literal length against the remaining output. It takesnintand compares asnuint, so a negative length is rejected as too long. - After decoding a long literal length, reject a length
<= 0on x86.IntPtr.Sizeis a JIT-time constant, so the check is removed on 64-bit, where the length can't overflow. (From #155.) - Decode the literal length in the scratch path the same way as the main loop: as
nint, with the same x86 check. This replaces theuintdecode that wrapped, and keeps the two paths consistent. - Build the
ExtractLowBytesfallback mask from a 64-bit value, and move the fallback intoExtractLowBytesFallbackso it is unit tested on hardware with BMI2. - The new checks are only on the split-literal and long-literal paths; the hot path is unchanged.
Tests
Three corrupt blocks, each run through Decompress, TryDecompress, DecompressToMemory, DecompressToArray, and the ReadOnlySequence overloads split at every segment size:
TruncatedLiteralBlock: the block from #152, an 18-byte literal with only 10 bytes of input.LiteralLongerThanUncompressedLengthBlock: a 14-byte literal with all bytes present, but a declared length of 10. Before this fix it was silently truncated to 10 bytes.LongLiteralLengthOverflowBlock: a long literal with a 4-byte length of0xFFFFFFFFfollowed by a valid 1-byte literal, with a declared length of 1. Before this fix the split cases decoded it successfully, and so did every case without BMI2 or on x86.
From #155, BadData_LongLiteralLength_ThrowsInvalidDataException covers 4-byte literal lengths of 0x7FFFFFFF, 0x80000000, 0xFFFFFFFE and 0xFFFFFFFF with the literal data present, so the tag goes through the main loop.
HelpersTests covers ExtractLowBytes and ExtractLowBytesFallback for 0 through 4 bytes; the 4-byte fallback cases fail on the old logic.
The IBufferWriter assertion runs on .NET 6+ only, matching the existing buffer writer tests, since ArrayBufferWriter<T> is not available on .NET Framework.
Supersedes #155.
š¤ Generated with Claude Code