Commit b1797390 authored by Joe Tsai's avatar Joe Tsai Committed by Brad Fitzpatrick

compress/zlib: 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: I86c497519daaaccefc6eb5617ddcd8fd3b99f51b
Reviewed-on: https://go-review.googlesource.com/14835Reviewed-by: default avatarNigel Tao <nigeltao@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 0c1f0549
...@@ -101,6 +101,9 @@ func (z *reader) Read(p []byte) (n int, err error) { ...@@ -101,6 +101,9 @@ func (z *reader) Read(p []byte) (n int, err error) {
// Finished file; check checksum. // Finished file; check checksum.
if _, err := io.ReadFull(z.r, z.scratch[0:4]); err != nil { if _, err := io.ReadFull(z.r, z.scratch[0:4]); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
z.err = err z.err = err
return 0, err return 0, err
} }
...@@ -130,6 +133,9 @@ func (z *reader) Reset(r io.Reader, dict []byte) error { ...@@ -130,6 +133,9 @@ func (z *reader) Reset(r io.Reader, dict []byte) error {
} }
_, err := io.ReadFull(z.r, z.scratch[0:2]) _, err := io.ReadFull(z.r, z.scratch[0:2])
if err != nil { if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err return err
} }
h := uint(z.scratch[0])<<8 | uint(z.scratch[1]) h := uint(z.scratch[0])<<8 | uint(z.scratch[1])
...@@ -140,6 +146,9 @@ func (z *reader) Reset(r io.Reader, dict []byte) error { ...@@ -140,6 +146,9 @@ func (z *reader) Reset(r io.Reader, dict []byte) error {
if haveDict { if haveDict {
_, err = io.ReadFull(z.r, z.scratch[0:4]) _, err = io.ReadFull(z.r, z.scratch[0:4])
if err != nil { if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err return err
} }
checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
......
...@@ -22,6 +22,30 @@ type zlibTest struct { ...@@ -22,6 +22,30 @@ type zlibTest struct {
// http://www.zlib.net/zpipe.c // http://www.zlib.net/zpipe.c
var zlibTests = []zlibTest{ var zlibTests = []zlibTest{
{
"truncated empty",
"",
[]byte{},
nil,
io.ErrUnexpectedEOF,
},
{
"truncated dict",
"",
[]byte{0x78, 0xbb},
[]byte{0x00},
io.ErrUnexpectedEOF,
},
{
"truncated checksum",
"",
[]byte{0x78, 0xbb, 0x00, 0x01, 0x00, 0x01, 0xca, 0x48,
0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x28, 0xcf, 0x2f,
0xca, 0x49, 0x01, 0x04, 0x00, 0x00, 0xff, 0xff,
},
[]byte{0x00},
io.ErrUnexpectedEOF,
},
{ {
"empty", "empty",
"", "",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment