From 152a08c531a3219489136df71a8d75ff9ba3bf06 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 29 Mar 2016 23:37:59 -0700 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/compress/gzip/gunzip.go | 42 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/compress/gzip/gunzip.go b/src/compress/gzip/gunzip.go index 4a4f19443d2..8ab2b5e5ecf 100644 --- a/src/compress/gzip/gunzip.go +++ b/src/compress/gzip/gunzip.go @@ -262,16 +262,13 @@ func (z *Reader) Read(p []byte) (n int, err error) { if z.err != nil { 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.size += uint32(n) - if n != 0 || err != io.EOF { - z.err = err - return + if z.err != io.EOF { + // In the normal case we return here. + return n, z.err } // Finished file; check checksum + size. @@ -280,28 +277,31 @@ func (z *Reader) Read(p []byte) (n int, err error) { err = io.ErrUnexpectedEOF } z.err = err - return 0, err + return n, err } crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8]) sum := z.digest.Sum32() if sum != crc32 || isize != z.size { 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.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) }