From 7a4327eb7a5941ff1c0852b894d24631baf390fc Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Sat, 15 Jul 2017 10:30:38 -0600 Subject: [PATCH] cmd/go-contrib-init: don't crash from src/golang.org/x If go-contrib-init is executed from the src/golang.org/x directory (as opposed to a sub-directory), it crashes with an index out-of-bounds. Fix this by including the trailing slash in the inspected path prefix so we never slice more than what we know is in the string. Change-Id: Ibbb74759c2ba839972de34a8ffb28b6cfb6825e2 Reviewed-on: https://go-review.googlesource.com/48690 Reviewed-by: Brad Fitzpatrick --- cmd/go-contrib-init/contrib.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/go-contrib-init/contrib.go b/cmd/go-contrib-init/contrib.go index 1cd47f84..64c79178 100644 --- a/cmd/go-contrib-init/contrib.go +++ b/cmd/go-contrib-init/contrib.go @@ -49,9 +49,9 @@ func detectrepo() string { } for _, path := range filepath.SplitList(build.Default.GOPATH) { - rightdir := filepath.Join(path, "src", "golang.org", "x") + rightdir := filepath.Join(path, "src", "golang.org", "x") + string(os.PathSeparator) if strings.HasPrefix(wd, rightdir) { - tail := wd[len(rightdir)+1:] + tail := wd[len(rightdir):] end := strings.Index(tail, string(os.PathSeparator)) if end > 0 { repo := tail[:end]