Repository navigation
Description
Follow-up to #1326.
When a skippable chunk is cut short and the SkippableCB callback reads past its end, the callback gets io.EOF from the io.LimitedReader. If the callback returns that error, skippable stores it in r.err, so Read returns io.EOF. io.ReadAll then reports success for a truncated stream. Skip and DecodeConcurrent return io.EOF where they should return ErrCorrupt.
#1326 covers a callback that returns nil: the drain and the rd.N > 0 check catch the truncation. Its test cuts the chunk after 50 bytes, and the callback uses io.ReadAll, so the callback never sees io.EOF.
On master (29e5455):
zz_cbeof_test.go:38: Read: got <nil>, want ErrCorrupt
zz_cbeof_test.go:38: Skip: got EOF, want ErrCorrupt
zz_cbeof_test.go:38: DecodeConcurrent: got EOF, want ErrCorrupt
Repro (s2/zz_cbeof_test.go)
package s2
import (
"bytes"
"io"
"testing"
)
func TestSkippableCBEOF(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
if err := w.AddSkippableBlock(0x80, bytes.Repeat([]byte("m"), 100)); err != nil {
t.Fatal(err)
}
if _, err := w.Write([]byte("some data")); err != nil {
t.Fatal(err)
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
// Stream identifier, then the 100-byte skippable chunk cut short after 5 bytes.
stream := buf.Bytes()[:4+len(magicBody)+4+5]
for _, tc := range []struct {
name string
op func(*Reader) error
}{
{"Read", func(r *Reader) error { _, err := io.ReadAll(r); return err }},
{"Skip", func(r *Reader) error { return r.Skip(1) }},
{"DecodeConcurrent", func(r *Reader) error { _, err := r.DecodeConcurrent(io.Discard, 2); return err }},
} {
r := NewReader(bytes.NewReader(stream))
r.SkippableCB(0x80, func(sr io.Reader) error {
_, err := io.CopyN(io.Discard, sr, 10)
return err
})
if err := tc.op(r); err != ErrCorrupt {
t.Errorf("%s: got %v, want ErrCorrupt", tc.name, err)
}
}
}Suggested fix in skippable. With it, the repro passes. Other callback errors pass through unchanged:
rd := &io.LimitedReader{R: r.r, N: int64(n)}
r.err = fn(rd)
+ if r.err == io.EOF && rd.N > 0 {
+ // The stream ended inside the chunk.
+ r.err = ErrCorrupt
+ }
if r.err != nil {
return false
}The test in #1326 could run each case with cuts after both 50 and 5 bytes, using a callback that reads 10 bytes. minio/minlz#112 does that for the MinLZ port.
A callback that reads the whole chunk and then returns io.EOF still ends the stream without an error. The docs say any non-nil error aborts decompression, so that is a bug in the callback, and this fix leaves it alone.
Activity
s2: report ErrCorrupt when skippable callback returns io.EOF on trunc…