]> Cypherpunks repositories - gostls13.git/commitdiff
compress/flate: detect truncated streams
authorJoe Tsai <joetsai@digital-static.net>
Tue, 22 Sep 2015 08:28:09 +0000 (01:28 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 23 Sep 2015 08:28:17 +0000 (08:28 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/compress/flate/flate_test.go
src/compress/flate/inflate.go

index 3f67025cd76b5f98ba26bc615dd87a80d28bd583..f2362dd84fd2fcbb0ada6b40c845ac855b606782 100644 (file)
@@ -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)
+               }
+       }
+}
index 09f61158048febb24cf0fcad2f6bd7cf4b684447..cbc0181240b917f860360b98f661733b37afd3af 100644 (file)
@@ -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
                }