[go] cmd/go: fix panic in 'go run -C' when flag value is missing

1 view
Skip to first unread message

Ravi Sastry Kadali (Gerrit)

unread,
Feb 6, 2026, 8:53:33 PM (5 hours ago) Feb 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ravi Sastry Kadali has uploaded the change for review

Commit message

cmd/go: fix panic in 'go run -C' when flag value is missing

When running 'go run -C' without providing the required directory
argument, `maybeSwitchForGoInstallVersion` panics with a slice bounds
out of range error because it attempts to skip the next argument for
a non-boolean flag without checking whether any arguments remain.

Add a bounds check before consuming the flag's value argument. If no
arguments remain, return early and let the normal command flag parser
report the missing argument error.

Fixes #77483
Change-Id: Ib3d1400df1e641b4ce4a9c353e60dedf6756887e

Change diff

diff --git a/src/cmd/go/internal/toolchain/select.go b/src/cmd/go/internal/toolchain/select.go
index 0cb93f6..4792b47 100644
--- a/src/cmd/go/internal/toolchain/select.go
+++ b/src/cmd/go/internal/toolchain/select.go
@@ -659,6 +659,9 @@

if bf, ok := f.Value.(interface{ IsBoolFlag() bool }); !ok || !bf.IsBoolFlag() {
// The next arg is the value for this flag. Skip it.
+ if len(args) == 0 {
+ return
+ }
args = args[1:]
continue
}
diff --git a/src/cmd/go/testdata/script/run_flag_missing_arg.txt b/src/cmd/go/testdata/script/run_flag_missing_arg.txt
new file mode 100644
index 0000000..bb6d495
--- /dev/null
+++ b/src/cmd/go/testdata/script/run_flag_missing_arg.txt
@@ -0,0 +1,8 @@
+! go run -C
+stderr 'flag needs an argument: -C'
+! stderr 'panic'
+
+# Same for 'go install -C'.
+! go install -C
+stderr 'flag needs an argument: -C'
+! stderr 'panic'
\ No newline at end of file

Change information

Files:
  • M src/cmd/go/internal/toolchain/select.go
  • A src/cmd/go/testdata/script/run_flag_missing_arg.txt
Change size: S
Delta: 2 files changed, 11 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib3d1400df1e641b4ce4a9c353e60dedf6756887e
Gerrit-Change-Number: 742940
Gerrit-PatchSet: 1
Gerrit-Owner: Ravi Sastry Kadali <ravis...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Ravi Sastry Kadali (Gerrit)

unread,
Feb 6, 2026, 9:01:16 PM (5 hours ago) Feb 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ravi Sastry Kadali added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Ravi Sastry Kadali . resolved

This is fixed in https://go-review.googlesource.com/c/go/+/742860 so this can be safely closed

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib3d1400df1e641b4ce4a9c353e60dedf6756887e
Gerrit-Change-Number: 742940
Gerrit-PatchSet: 1
Gerrit-Owner: Ravi Sastry Kadali <ravis...@gmail.com>
Gerrit-Comment-Date: Sat, 07 Feb 2026 02:01:12 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Ravi Sastry Kadali (Gerrit)

unread,
Feb 6, 2026, 9:01:44 PM (5 hours ago) Feb 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ravi Sastry Kadali abandoned this change.

View Change

Abandoned https://go-review.googlesource.com/c/go/+/742860 fixes the issue. So this can safely be abandoned

Ravi Sastry Kadali abandoned this change

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: abandon
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages