pierrec / lz4

LZ4 compression and decompression in pure Go
BSD 3-Clause "New" or "Revised" License
878 stars 142 forks source link

Portable decoder doesn't return error for output buffer too short #167

Closed greatroar closed 2 years ago

greatroar commented 2 years ago

The following tries to decode into a buffer that is too short:

// 14 bytes literal, 3+minMatch=7 bytes match, for 21 bytes total.
src := []byte("\xe300000000000000\t\x00\x00")
dst := make([]byte, 18)
lz4.UncompressBlock(src, dst)

The asm decoders correctly detect that the output buffer is too short. The portable decoder returns 21, nil.

The problem is in "Shortcut 2" of the portable decoder. Commenting that out makes it work. It should probably require more space in dst, but that was removed as part of the fix for #51.

greatroar commented 2 years ago

In fact, just commenting out

if end > uint(len(dst)) {
    // The remaining buffer may not hold 18 bytes.
    // See https://github.com/pierrec/lz4/issues/51.
    end = uint(len(dst))
}

makes it work, without TestIssue51 failing.