From 9deb940d2481396944f090dd9f205f2d5b3d94ee Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 22 Sep 2015 01:28:09 -0700 Subject: [PATCH] compress/flate: detect truncated streams Reader failed to detect truncated streams since calls to io.ReadFull did not check if the error is io.EOF. Change-Id: I0634e0d8de1ab04e8f93242c27a9f89e57743e87 Reviewed-on: https://go-review.googlesource.com/14833 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/compress/flate/flate_test.go | 17 +++++++++++++++++ src/compress/flate/inflate.go | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/src/compress/flate/flate_test.go b/src/compress/flate/flate_test.go index 3f67025cd7..f2362dd84f 100644 --- a/src/compress/flate/flate_test.go +++ b/src/compress/flate/flate_test.go @@ -11,7 +11,9 @@ package flate import ( "bytes" "encoding/hex" + "io" "io/ioutil" + "strings" "testing" ) @@ -258,3 +260,18 @@ func TestStreams(t *testing.T) { } } } + +func TestTruncatedStreams(t *testing.T) { + const data = "\x00\f\x00\xf3\xffhello, world\x01\x00\x00\xff\xff" + + for i := 0; i < len(data)-1; i++ { + r := NewReader(strings.NewReader(data[:i])) + _, err := io.Copy(ioutil.Discard, r) + if ferr, ok := err.(*ReadError); ok { + err = ferr.Err + } + if err != io.ErrUnexpectedEOF { + t.Errorf("io.Copy(%d) on truncated stream: got %v, want %v", i, err, io.ErrUnexpectedEOF) + } + } +} diff --git a/src/compress/flate/inflate.go b/src/compress/flate/inflate.go index 09f6115804..cbc0181240 100644 --- a/src/compress/flate/inflate.go +++ b/src/compress/flate/inflate.go @@ -637,6 +637,9 @@ func (f *decompressor) dataBlock() { nr, err := io.ReadFull(f.r, f.buf[0:4]) f.roffset += int64(nr) if err != nil { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } f.err = &ReadError{f.roffset, err} return } @@ -669,6 +672,9 @@ func (f *decompressor) copyData() { m, err := io.ReadFull(f.r, f.hist[f.hp:f.hp+m]) f.roffset += int64(m) if err != nil { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } f.err = &ReadError{f.roffset, err} return }