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: uses more space in buffer than returned in EncodedLen method #53606

Closed
mateusz834 opened this issue Jun 29, 2022 · 6 comments
Closed

Comments

@mateusz834
Copy link
Member

mateusz834 commented Jun 29, 2022

What version of Go are you using (go version)?

$ go version
go version go1.18.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
go version go1.18.3 linux/amd64
[mateusz@arch example ]$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/mateusz/go/bin"
GOCACHE="/home/mateusz/.cache/go-build"
GOENV="/home/mateusz/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mateusz/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mateusz/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mateusz/code/example/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1154144156=/tmp/go-build -gno-record-gcc-switches"

What did you do?

func main() {
	enc := base32.HexEncoding.WithPadding(base32.NoPadding)

	data := []byte{0: 255, 15: 255}

	fmt.Println(enc.EncodedLen(len(data))) //26

	buf := [128]byte{}

	enc.Encode(buf[:], data)
	fmt.Printf("%v\n", string(buf[:]))  //VS0000000000000000000000VS000000
	fmt.Println(enc.EncodeToString(data)) //VS0000000000000000000000VS
}

https://go.dev/play/p/nv53zKwHz3E

What did you expect to see?

fmt.Printf("%v\n", string(buf[:]))   //VS0000000000000000000000VS
fmt.Println(enc.EncodeToString(data)) //VS0000000000000000000000VS

What did you see instead?

fmt.Printf("%v\n", string(buf[:]))   //VS0000000000000000000000VS000000
fmt.Println(enc.EncodeToString(data)) //VS0000000000000000000000VS
@bcmills
Copy link
Contributor

bcmills commented Jun 29, 2022

The Encode method seems to be working as documented. See the example at https://pkg.go.dev/encoding/base32#example-Encoding.Encode.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see the resources listed at https://go.dev/wiki/Questions.

@bcmills bcmills closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2022
@mateusz834
Copy link
Member Author

mateusz834 commented Jun 29, 2022

Docs explicit state "writing EncodedLen(len(src)) bytes to dst", but it the example above it is writing EncodedLen(len(src)) +6 Bytes

@bcmills
Copy link
Contributor

bcmills commented Jun 29, 2022

Writing bytes into a slice does not resize the slice.

@mateusz834
Copy link
Member Author

So then why the encoding/base64 does not append '0' characters at the end?
https://go.dev/play/p/gRnZ-1lEXgw

@mateusz834
Copy link
Member Author

mateusz834 commented Jun 29, 2022

In some weird cases it could lead to security issues: https://go.dev/play/p/V-Mj6YDR_E4

@ianlancetaylor
Copy link
Contributor

For questions, please see https://go.dev/wiki/Questions. Thanks.

@golang golang locked and limited conversation to collaborators Jun 29, 2023
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

4 participants