1
0
mirror of https://github.com/golang/go synced 2024-09-30 04:34:33 -06: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
This commit is contained in:
Gustav Westling 2018-05-09 18:59:40 +02:00 committed by Gustav Westling
parent 814c749c8f
commit f374096d2f
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

@ -578,3 +578,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)
}
}
}
}