Skip to content

LZ4DecompressorWithLength safe paths accept output shorter than the declared length #105

Closed

Description

@yawkat

If the 4-byte length prefix says N, but the data decompresses to M < N bytes, the safe overloads of LZ4DecompressorWithLength don't reject it:

  • decompress(byte[] src, int srcOff, int srcLen) (line 301) silently returns a truncated M-byte array. LZ4SafeDecompressor.decompress(.., maxDestLen) trims it with Arrays.copyOf.
  • The non-allocating byte[]/ByteBuffer overloads (lines 241, 375) return written = M. A caller that trusts getDecompressedLength(src) instead of the return value reads dest[destOff+M .. destOff+N), which is whatever was in its own buffer before, for example a pooled buffer.
  • decompress(ByteBuffer, ByteBuffer) (line 321) advances dest by M and silently accepts the short output.

The fast paths already require exactly destLen bytes.

Fix: in the safe paths, throw LZ4Exception when written != destLen, so they behave like the fast paths.

private static int checkWritten(int written, int declared) {
  if (written != declared) throw new LZ4Exception(
      "Decompressed " + written + " bytes, but length prefix declared " + declared);
  return written;
}

In the ByteBuffer, ByteBuffer overload, check before updating the positions. Update the @return Javadoc wording to match.

Add a test that compresses 16 bytes, rewrites the prefix to 20, and asserts that every safe overload throws.

Activity

  1. self-assigned this
    on Sep 25, 2026
  2. added
    bug
    on Sep 25, 2026
  3. added 2 commits that reference this issue on Sep 25, 2026
    02e70af
    681f503
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions