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

go/version: slice bounds out of range in Lang (only at tip) #64033

Closed
catenacyber opened this issue Nov 9, 2023 · 5 comments
Closed

go/version: slice bounds out of range in Lang (only at tip) #64033

catenacyber opened this issue Nov 9, 2023 · 5 comments
Assignees
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@catenacyber
Copy link
Contributor

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

$ go version
go version go1.21 linux/amd64

Does this issue reproduce with the latest release?

It happens only on gotip, not on go 1.21

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/root/.go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/root/.go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.21"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/src/ngolo-fuzzing/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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2481516251=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run https://go.dev/play/p/Y_28F1xBFWu?v=gotip
ie version.Lang("go222")

What did you expect to see?

The program finishing and printing Hello

What did you see instead?

panic: runtime error: slice bounds out of range [:7] with length 5

goroutine 1 [running]:
go/version.Lang({0x494c74, 0x5})
	/usr/local/go-faketime/src/go/version/version.go:36 +0x7a
main.main()
	/tmp/sandbox1087001645/prog.go:11 +0x1f

Program exited.

Found by https://github.com/catenacyber/ngolo-fuzzing with oss-fuzz :
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=64002

@mateusz834 mateusz834 added the NeedsFix The path to resolution is known, but the work has not been done. label Nov 9, 2023
@mateusz834 mateusz834 added this to the Go1.22 milestone Nov 9, 2023
@mateusz834
Copy link
Member

mateusz834 commented Nov 9, 2023

This happens, because gover.Lang(stripGo("go222")) returns "go222.0".

This will fix the issue:

func Lang(x string) string {
	v := gover.Lang(stripGo(x))
	if v == "" {
		return ""
	}
+       if len(x) < 2+len(v) {
+		return "go" + v
+	}
	return x[:2+len(v)] // "go"+v without allocation
}

It does not panic for "go1", because of this:

if v.Minor == "" || v.Major == "1" && v.Minor == "0" {
return v.Major
}

and v.Minor is never "", because of
v.Major, x, ok = cutInt(x)
if !ok {
return Version{}
}
if x == "" {
// Interpret "1" as "1.0.0".
v.Minor = "0"
v.Patch = "0"
return v
}

This might also fix the issue, but differently. This should for "go222" return "go222" instead of "go222.0".

// Lang returns the Go language version. For example, Lang("1.2.3") == "1.2".
func Lang(x string) string {
	v := Parse(x)
-	if v.Minor == "" || v.Major == "1" && v.Minor == "0" {
+	if v.Minor == "" || v.Minor == "0" {
		return v.Major
	}
	return v.Major + "." + v.Minor
}

I am not sending a CL, because I am not sure which way this should be handled.

@mateusz834
Copy link
Member

CC @rsc

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/541915 mentions this issue: gover: support Semantic Versioning major versions beyond 1

@bcmills bcmills assigned bcmills and unassigned rsc Nov 17, 2023
@bcmills bcmills added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label Dec 1, 2023
gopherbot pushed a commit that referenced this issue Dec 6, 2023
For #64033

Change-Id: Iab132f86c66aa6115a349d8032e9766a14dad02e
Reviewed-on: https://go-review.googlesource.com/c/go/+/541915
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
@bcmills
Copy link
Contributor

bcmills commented Dec 8, 2023

I believe this is fixed by https://go.dev/cl/541915.

@catenacyber
Copy link
Contributor Author

Looks fixed indeed on my side

ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
For golang#64033

Change-Id: Iab132f86c66aa6115a349d8032e9766a14dad02e
Reviewed-on: https://go-review.googlesource.com/c/go/+/541915
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
@golang golang locked and limited conversation to collaborators Dec 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
Status: Done
Development

No branches or pull requests

5 participants