From: Nigel Tao Date: Thu, 27 Sep 2012 03:29:39 +0000 (+1000) Subject: lzw: fix Write returning the wrong number of bytes written. X-Git-Tag: go1.1rc2~2306 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=791ac65b8271ad374df3877d94c3b5eee9c09537;p=gostls13.git lzw: fix Write returning the wrong number of bytes written. Fixes #4160. R=rsc, r CC=golang-dev https://golang.org/cl/6564060 --- diff --git a/src/pkg/compress/lzw/writer.go b/src/pkg/compress/lzw/writer.go index 488ba6428d..c6f891b4bc 100644 --- a/src/pkg/compress/lzw/writer.go +++ b/src/pkg/compress/lzw/writer.go @@ -131,13 +131,14 @@ func (e *encoder) incHi() error { } // Write writes a compressed representation of p to e's underlying writer. -func (e *encoder) Write(p []byte) (int, error) { +func (e *encoder) Write(p []byte) (n int, err error) { if e.err != nil { return 0, e.err } if len(p) == 0 { return 0, nil } + n = len(p) litMask := uint32(1< e.hi into the map that e.table represents. @@ -184,7 +185,7 @@ loop: } } e.savedCode = code - return len(p), nil + return n, nil } // Close closes the encoder, flushing any pending output. It does not close or diff --git a/src/pkg/compress/lzw/writer_test.go b/src/pkg/compress/lzw/writer_test.go index 97daf1406a..3e4e6de211 100644 --- a/src/pkg/compress/lzw/writer_test.go +++ b/src/pkg/compress/lzw/writer_test.go @@ -96,6 +96,14 @@ func TestWriter(t *testing.T) { } } +func TestWriterReturnValues(t *testing.T) { + w := NewWriter(ioutil.Discard, LSB, 8) + n, err := w.Write([]byte("asdf")) + if n != 4 || err != nil { + t.Errorf("got %d, %v, want 4, nil", n, err) + } +} + func benchmarkEncoder(b *testing.B, n int) { b.StopTimer() b.SetBytes(int64(n))