1
0
mirror of https://github.com/golang/go synced 2024-11-17 22:14:43 -07:00

encoding/base32: handle NoPadding when using buffered encoding in Close

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: f374096d2f
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>
This commit is contained in:
Gustav Westling 2018-05-09 19:05:46 +00:00 committed by Ian Lance Taylor
parent 0f2d4d0008
commit 10529a01fd
2 changed files with 30 additions and 1 deletions

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

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