]> Cypherpunks repositories - gostls13.git/commitdiff
lzw: fix Write returning the wrong number of bytes written.
authorNigel Tao <nigeltao@golang.org>
Thu, 27 Sep 2012 03:29:39 +0000 (13:29 +1000)
committerNigel Tao <nigeltao@golang.org>
Thu, 27 Sep 2012 03:29:39 +0000 (13:29 +1000)
Fixes #4160.

R=rsc, r
CC=golang-dev
https://golang.org/cl/6564060

src/pkg/compress/lzw/writer.go
src/pkg/compress/lzw/writer_test.go

index 488ba6428db63703b1f056d81113c98f3c447470..c6f891b4bc0231b42fec66c750499d5c3247ddcd 100644 (file)
@@ -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.litWidth - 1)
        code := e.savedCode
        if code == invalidCode {
@@ -167,11 +168,11 @@ loop:
                code = literal
                // Increment e.hi, the next implied code. If we run out of codes, reset
                // the encoder state (including clearing the hash table) and continue.
-               if err := e.incHi(); err != nil {
-                       if err == errOutOfCodes {
+               if err1 := e.incHi(); err1 != nil {
+                       if err1 == errOutOfCodes {
                                continue
                        }
-                       e.err = err
+                       e.err = err1
                        return 0, e.err
                }
                // Otherwise, insert key -> 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
index 97daf1406a50c117d34e60ca95c92282f0b83cc1..3e4e6de2114772ebd51ced82f45ca5f2342b4269 100644 (file)
@@ -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))