]> Cypherpunks repositories - gostls13.git/commitdiff
compress/lzw: return the partial decoding for a truncated input.
authorNigel Tao <nigeltao@golang.org>
Mon, 29 Jun 2015 06:22:54 +0000 (16:22 +1000)
committerNigel Tao <nigeltao@golang.org>
Tue, 30 Jun 2015 03:47:06 +0000 (03:47 +0000)
This is needed by issue #9856.

Change-Id: Idad570a7e55ad903aab55372d390bc746c4e19cf
Reviewed-on: https://go-review.googlesource.com/11661
Reviewed-by: Rob Pike <r@golang.org>
src/compress/lzw/reader.go
src/compress/lzw/reader_test.go

index 28e9f7514f7f3d09a153fd6acaf180ea458e6417..1353831eca9d8ad4d9a243da5f8cfc6cef1ae31c 100644 (file)
@@ -139,6 +139,7 @@ func (d *decoder) decode() {
                                err = io.ErrUnexpectedEOF
                        }
                        d.err = err
+                       d.flush()
                        return
                }
                switch {
@@ -190,6 +191,7 @@ func (d *decoder) decode() {
                        }
                default:
                        d.err = errors.New("lzw: invalid code")
+                       d.flush()
                        return
                }
                d.last, d.hi = code, d.hi+1
index 9006c91c233c62bf5ca585c5c652451322223a1b..c3a5c3a0aaacdf3bfce7fbbf111786cb9310e98a 100644 (file)
@@ -98,13 +98,20 @@ func TestReader(t *testing.T) {
                defer rc.Close()
                b.Reset()
                n, err := io.Copy(&b, rc)
+               s := b.String()
                if err != nil {
                        if err != tt.err {
                                t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err)
                        }
+                       if err == io.ErrUnexpectedEOF {
+                               // Even if the input is truncated, we should still return the
+                               // partial decoded result.
+                               if n == 0 || !strings.HasPrefix(tt.raw, s) {
+                                       t.Errorf("got %d bytes (%q), want a non-empty prefix of %q", n, s, tt.raw)
+                               }
+                       }
                        continue
                }
-               s := b.String()
                if s != tt.raw {
                        t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw)
                }