From 791ac65b8271ad374df3877d94c3b5eee9c09537 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 27 Sep 2012 13:29:39 +1000 Subject: [PATCH] lzw: fix Write returning the wrong number of bytes written. Fixes #4160. R=rsc, r CC=golang-dev https://golang.org/cl/6564060 --- src/pkg/compress/lzw/writer.go | 11 ++++++----- src/pkg/compress/lzw/writer_test.go | 8 ++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) 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)) -- 2.48.1