1
0
mirror of https://github.com/golang/go synced 2024-11-26 22:21:27 -07:00

compress/lzw: return the partial decoding for a truncated input.

This is needed by issue #9856.

Change-Id: Idad570a7e55ad903aab55372d390bc746c4e19cf
Reviewed-on: https://go-review.googlesource.com/11661
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Nigel Tao 2015-06-29 16:22:54 +10:00
parent 53eb4783c2
commit fea18f5a34
2 changed files with 10 additions and 1 deletions

View 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

View 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)
}