1
0
mirror of https://github.com/golang/go synced 2024-11-19 01:04:40 -07:00

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/21308
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 2016-03-29 23:37:59 -07:00 committed by Brad Fitzpatrick
parent 6b97dbf848
commit 152a08c531

View File

@ -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) {
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
} }
// File is ok; is there another?
if !z.multistream {
return 0, io.EOF
}
if err = z.readHeader(false); err != nil {
z.err = err
return
}
// Yes. Reset and read from it.
z.digest.Reset() z.digest.Reset()
z.size = 0 z.size = 0
// File is ok; check if there is another.
if !z.multistream {
return n, io.EOF
}
z.err = nil // Remove io.EOF
if z.err = z.readHeader(false); z.err != nil {
return n, z.err
}
// Read from next file, if necessary.
if n > 0 {
return n, nil
}
return z.Read(p) return z.Read(p)
} }