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
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
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
This is fixed in https://go-review.googlesource.com/c/go/+/742860 so this can be safely closed
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
Ravi Sastry Kadali abandoned this change.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |