You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
encoding/base32's NewEncoder/Write/Close differ from Encode/EncodeToString when using an Encoding that has NoPadding set. Encode/EncodeToString correctly truncate the string at the point where padding characters would appear. NewEncoder/Write/Close always write a multiple of 8 bytes, as if padding with 'A', which leads to spurious trailing '\x00' bytes after decoding.
I think the bug lies in encoder.Close: it doesn't know about NoPadding, always outputting either 0 or 8 bytes.
What version of Go are you using (go version)?
go version go1.10.1 linux/amd64
Does this issue reproduce with the latest release?
Yes (reproduces with go1.10.2 on play.golang.org).
What operating system and processor architecture are you using (go env)?
package main
import (
"bytes"
"encoding/base32"
"fmt"
)
var input = []byte("foobar")
// Create a new base32 encoder, write src through it, and return the resulting
// string.
func thruEncoder(encoding *base32.Encoding, src []byte) string {
var buf bytes.Buffer
encoder := base32.NewEncoder(encoding, &buf)
encoder.Write(src)
encoder.Close()
return buf.String()
}
func main() {
withPadding := base32.StdEncoding
withoutPadding := base32.StdEncoding.WithPadding(base32.NoPadding)
fmt.Printf(" with padding EncodeToString: %q\n", withPadding.EncodeToString(input))
fmt.Printf(" with padding NewEncoder : %q\n", thruEncoder(withPadding, input))
fmt.Printf("without padding EncodeToString: %q\n", withoutPadding.EncodeToString(input))
fmt.Printf("without padding NewEncoder : %q\n", thruEncoder(withoutPadding, input))
}
What did you expect to see?
with padding EncodeToString: "MZXW6YTBOI======"
with padding NewEncoder : "MZXW6YTBOI======"
without padding EncodeToString: "MZXW6YTBOI"
without padding NewEncoder : "MZXW6YTBOI"
What did you see instead?
with padding EncodeToString: "MZXW6YTBOI======"
with padding NewEncoder : "MZXW6YTBOI======"
without padding EncodeToString: "MZXW6YTBOI"
without padding NewEncoder : "MZXW6YTBOIAAAAAA"
The text was updated successfully, but these errors were encountered:
josharian
changed the title
base32 encoder erroneously pads with 'A' when NoPadding is set
encoding/base32: encoder erroneously pads with 'A' when NoPadding is set
May 8, 2018
encoding/base32's
NewEncoder
/Write
/Close
differ fromEncode
/EncodeToString
when using anEncoding
that hasNoPadding
set.Encode
/EncodeToString
correctly truncate the string at the point where padding characters would appear.NewEncoder
/Write
/Close
always write a multiple of 8 bytes, as if padding with'A'
, which leads to spurious trailing'\x00'
bytes after decoding.I think the bug lies in
encoder.Close
: it doesn't know aboutNoPadding
, always outputting either 0 or 8 bytes.What version of Go are you using (
go version
)?go version go1.10.1 linux/amd64
Does this issue reproduce with the latest release?
Yes (reproduces with go1.10.2 on play.golang.org).
What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/nnHxBOHHHDV
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: