aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeal Patel <nealpatel@google.com>2026-04-11 12:00:52 -0400
committerGopher Robot <gobot@golang.org>2026-04-13 19:04:58 -0700
commit33241d7298e0c621cfc4cc9f878dba9eff2b1c3d (patch)
tree4931449a9af87f578a6cb4af11376f364e86d556
parentf35e6df70bf3cbb748657a58f9b5bb9a9a491b69 (diff)
downloadgo-33241d7298e0c621cfc4cc9f878dba9eff2b1c3d.tar.xz
os/exec: use argv() to avoid panic inside of Cmd.String()
A surprisingly non-zero amount of direct uses Cmd make this panic possible. Change-Id: If86cabfb0f7c0250e2a5aa3fcaba367de5d10ca4 Reviewed-on: https://go-review.googlesource.com/c/go/+/765680 Reviewed-by: Ian Lance Taylor <iant@golang.org> Auto-Submit: Neal Patel <nealpatel@google.com> TryBot-Bypass: Nicholas Husin <nsh@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Nicholas Husin <nsh@golang.org> Reviewed-by: Keith Randall <khr@google.com>
-rw-r--r--src/os/exec/exec.go2
-rw-r--r--src/os/exec/exec_test.go6
2 files changed, 7 insertions, 1 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go
index 26ddfe633c..24cd6b141a 100644
--- a/src/os/exec/exec.go
+++ b/src/os/exec/exec.go
@@ -516,7 +516,7 @@ func (c *Cmd) String() string {
// report the exact executable path (plus args)
b := new(strings.Builder)
b.WriteString(c.Path)
- for _, a := range c.Args[1:] {
+ for _, a := range c.argv()[1:] {
b.WriteByte(' ')
b.WriteString(a)
}
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index 2746ad8783..2d966b627e 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -1855,3 +1855,9 @@ func TestStart_twice(t *testing.T) {
t.Fatalf("Start call returned err %q, want %q", got, want)
}
}
+
+func TestNoStringPanic(t *testing.T) {
+ if s := fmt.Sprintf("%v", &exec.Cmd{}); strings.Contains(s, "PANIC") {
+ t.Fatalf("got %q, want no panic", s)
+ }
+}