Commit 152a08c5 authored by Joe Tsai's avatar Joe Tsai Committed by Brad Fitzpatrick

compress/gzip: fix error handling in Read

The Read logic should not assume that only (0, io.EOF) is returned
instead of (n, io.EOF) where n is positive.

The fix done here is very similar to the fix to compress/zlib
in CL/20292.

Change-Id: Icb76258cdcf8cfa386a60bab330fefde46fc071d
Reviewed-on: https://go-review.googlesource.com/21308Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 6b97dbf8
...@@ -262,16 +262,13 @@ func (z *Reader) Read(p []byte) (n int, err error) { ...@@ -262,16 +262,13 @@ func (z *Reader) Read(p []byte) (n int, err error) {
if z.err != nil { if z.err != nil {
return 0, z.err return 0, z.err
} }
if len(p) == 0 {
return 0, nil
}
n, err = z.decompressor.Read(p) n, z.err = z.decompressor.Read(p)
z.digest.Write(p[0:n]) z.digest.Write(p[0:n])
z.size += uint32(n) z.size += uint32(n)
if n != 0 || err != io.EOF { if z.err != io.EOF {
z.err = err // In the normal case we return here.
return return n, z.err
} }
// Finished file; check checksum + size. // Finished file; check checksum + size.
...@@ -280,28 +277,31 @@ func (z *Reader) Read(p []byte) (n int, err error) { ...@@ -280,28 +277,31 @@ func (z *Reader) Read(p []byte) (n int, err error) {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF
} }
z.err = err z.err = err
return 0, err return n, err
} }
crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8]) crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8])
sum := z.digest.Sum32() sum := z.digest.Sum32()
if sum != crc32 || isize != z.size { if sum != crc32 || isize != z.size {
z.err = ErrChecksum z.err = ErrChecksum
return 0, z.err return n, z.err
} }
z.digest.Reset()
z.size = 0
// File is ok; is there another? // File is ok; check if there is another.
if !z.multistream { if !z.multistream {
return 0, io.EOF return n, io.EOF
} }
z.err = nil // Remove io.EOF
if err = z.readHeader(false); err != nil { if z.err = z.readHeader(false); z.err != nil {
z.err = err return n, z.err
return
} }
// Yes. Reset and read from it. // Read from next file, if necessary.
z.digest.Reset() if n > 0 {
z.size = 0 return n, nil
}
return z.Read(p) return z.Read(p)
} }
......
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