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/base64: Decode panics on short dst #50117

Closed
a-h opened this issue Dec 12, 2021 · 3 comments
Closed

encoding/base64: Decode panics on short dst #50117

a-h opened this issue Dec 12, 2021 · 3 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@a-h
Copy link
Contributor

a-h commented Dec 12, 2021

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

  • go version go1.17.5 linux/arm64
  • go version go1.17.3 darwin/arm64

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

GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/adrian/Library/Caches/go-build"
GOENV="/Users/adrian/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/adrian/go/pkg/mod"
GONOPROXY=""
GONOSUMDB="*"
GOOS="darwin"
GOPATH="/Users/adrian/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/nix/store/346ngr086m8gdyarx2f61j7mrrg3zwzf-go-1.17.3/opt/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/nix/store/346ngr086m8gdyarx2f61j7mrrg3zwzf-go-1.17.3/opt/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/adrian/github.com/a-h/gobase64panic/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/41/f9l_y7sd0y99gnh9dt2z8sd80000gn/T/go-build1137988266=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {
	input := base64.RawStdEncoding.EncodeToString([]byte("12345"))
	var output []byte
	_, err := base64.RawStdEncoding.Decode(output, []byte(input))
	if err != nil {
		fmt.Println("Received expected error")
	}
}

What did you expect to see?

An error returned by the Decode function.

What did you see instead?

A panic. The documentation of the function doesn't say to expect a panic on invalid data.

@a-h
Copy link
Contributor Author

a-h commented Dec 12, 2021

Same issue with StdEncoding: https://go.dev/play/p/iGdwQ30aeng

@seankhliao
Copy link
Member

It's not invalid data, it's the short/nil output buffer. Maybe it can return io.ErrShortBuffer instead?

cc @rsc

@seankhliao seankhliao changed the title encoding/base64: unexpected panic on invalid base64 data encoding/base64: Decode panics on short dst Dec 12, 2021
@seankhliao seankhliao added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 12, 2021
@ianlancetaylor
Copy link
Member

Many standard library functions panic on invalid nil parameter values. I don't think there is anything to do here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants