cmd/go: reject an empty tool name

An empty tool name ("") is incorrectly resolved by "go tool" as the directory
containing the tools binaries:

  $ go tool ""
  go tool : fork/exec /opt/homebrew/Cellar/go/1.24.5/libexec/pkg/tool/darwin_arm64: permission denied

To fix that case we also explicitely disallow an empty tool name in the
cmd/go/internal/base.ValidToolName func.

Tests: go test cmd/go -v '-run=Script/^tool_name$'

Fixes #74757.

Change-Id: I6dd14096526c9113cef8e4d16a5aaa2120410b08
Reviewed-on: https://go-review.googlesource.com/c/go/+/690435
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Olivier Mengué
2025-07-25 10:34:34 +02:00
committed by Michael Matloob
parent 59bafc0b07
commit 86b5e678e8
2 changed files with 25 additions and 0 deletions

View File

@@ -42,6 +42,9 @@ func ToolPath(toolName string) (string, error) {
}
func ValidToolName(toolName string) bool {
if toolName == "" {
return false
}
for _, c := range toolName {
switch {
case 'a' <= c && c <= 'z', '0' <= c && c <= '9', c == '_':

View File

@@ -0,0 +1,22 @@
[short] skip 'runs go build'
# Tool name can't be empty. Issue #74757.
! go tool ''
stderr 'go: no such tool ""'
! go tool -n ''
stderr 'go: no such tool ""'
# Invalid tool name
! go tool @
stderr 'go: no such tool "@"'
! go tool -n @
stderr 'go: no such tool "@"'
-- go.mod --
module example.com/foo
go 1.24
-- main.go --
package main