1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:54:44 -07:00

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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Joe Tsai 2015-09-22 01:28:09 -07:00 committed by Brad Fitzpatrick
parent 646401bdf0
commit 9deb940d24
2 changed files with 23 additions and 0 deletions

View 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)
}
}
}

View 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
}