Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/base32: encoder erroneously pads with 'A' when NoPadding is set #25295

Closed
AxbB36 opened this issue May 8, 2018 · 2 comments
Closed
Milestone

Comments

@AxbB36
Copy link

AxbB36 commented May 8, 2018

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)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"

What did you do?

https://play.golang.org/p/nnHxBOHHHDV

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"
@josharian
Copy link
Contributor

cc @zegl

@josharian 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
@josharian josharian added this to the Go1.11 milestone May 8, 2018
zegl added a commit to zegl/go that referenced this issue May 9, 2018
This changes makes encoder.Close aware of how many bytes to write if there
is any data left in the buffer.

Fixes golang#25295
@gopherbot
Copy link

Change https://golang.org/cl/112515 mentions this issue: encoding/base32: handle NoPadding when using buffered encoding in Close

zegl added a commit to zegl/go that referenced this issue May 9, 2018
This changes makes encoder.Close aware of how many bytes to write if there
is any data left in the buffer.

Fixes golang#25295
@golang golang locked and limited conversation to collaborators May 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants