]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base32: handle NoPadding when using buffered encoding in Close
authorGustav Westling <zegl@westling.xyz>
Wed, 9 May 2018 19:05:46 +0000 (19:05 +0000)
committerIan Lance Taylor <iant@golang.org>
Wed, 9 May 2018 22:10:32 +0000 (22:10 +0000)
This changes makes encoder.Close aware of how many bytes to write if there
is any data left in the buffer.

Fixes #25295

Change-Id: I4138891359935509cb561c453b8059ba2b9e576b
GitHub-Last-Rev: f374096d2f3cae8635506074f59e1cd440c14844
GitHub-Pull-Request: golang/go#25316
Reviewed-on: https://go-review.googlesource.com/112515
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/encoding/base32/base32.go
src/encoding/base32/base32_test.go

index f3430654e1b486515565288ed5241d5bb41657fd..09e90eab5fff35ba1c6b36e132cbcf4f7a7d4d9e 100644 (file)
@@ -244,8 +244,9 @@ func (e *encoder) Close() error {
        // If there's anything left in the buffer, flush it out
        if e.err == nil && e.nbuf > 0 {
                e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
+               encodedLen := e.enc.EncodedLen(e.nbuf)
                e.nbuf = 0
-               _, e.err = e.w.Write(e.out[0:8])
+               _, e.err = e.w.Write(e.out[0:encodedLen])
        }
        return e.err
 }
index 094ac288d64174d1185ed3620ff51e8ba8974eae..fdd862dc499a49d20f14fe55e64fbeba70b952da 100644 (file)
@@ -658,3 +658,31 @@ func TestEncodedDecodedLen(t *testing.T) {
                })
        }
 }
+
+func TestWithoutPaddingClose(t *testing.T) {
+       encodings := []*Encoding{
+               StdEncoding,
+               StdEncoding.WithPadding(NoPadding),
+       }
+
+       for _, encoding := range encodings {
+               for _, testpair := range pairs {
+
+                       var buf bytes.Buffer
+                       encoder := NewEncoder(encoding, &buf)
+                       encoder.Write([]byte(testpair.decoded))
+                       encoder.Close()
+
+                       expected := testpair.encoded
+                       if encoding.padChar == NoPadding {
+                               expected = strings.Replace(expected, "=", "", -1)
+                       }
+
+                       res := buf.String()
+
+                       if res != expected {
+                               t.Errorf("Expected %s got %s; padChar=%d", expected, res, encoding.padChar)
+                       }
+               }
+       }
+}