diff options
author | Russ Cox <rsc@golang.org> | 2022-05-03 15:14:56 -0400 |
---|---|---|
committer | Alex Rakoczy <alex@golang.org> | 2022-05-27 14:57:10 +0000 |
commit | 6c65a4ab8e8bc6caa70d3bfe9b4bdd3dc26b8de5 (patch) | |
tree | 02ed569dd15d1611c85b51cc6c730193ebb000e3 | |
parent | a08baaad1a7c8e88b5a521ae12666b0aa3dc252a (diff) | |
download | go-6c65a4ab8e8bc6caa70d3bfe9b4bdd3dc26b8de5.tar.gz go-6c65a4ab8e8bc6caa70d3bfe9b4bdd3dc26b8de5.zip |
[release-branch.go1.18] os/exec: return clear error for missing cmd.Path
Following up on CL 403694, there is a bit of confusion about
when Path is and isn't set, along with now the exported Err field.
Catch the case where Path and Err (and lookPathErr) are all unset
and give a helpful error.
Updates #52574
Followup after #43724.
Fixes #53057
Fixes CVE-2022-30580
Change-Id: I03205172aef3801c3194f5098bdb93290c02b1b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/403759
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
(cherry picked from commit 960ffa98ce73ef2c2060c84c7ac28d37a83f345e)
Reviewed-on: https://go-review.googlesource.com/c/go/+/408577
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Roland Shoemaker <roland@golang.org>
-rw-r--r-- | src/os/exec/exec.go | 3 | ||||
-rw-r--r-- | src/os/exec/exec_test.go | 8 |
2 files changed, 11 insertions, 0 deletions
diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 845b737e28..ecddee690d 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -374,6 +374,9 @@ func lookExtensions(path, dir string) (string, error) { // The Wait method will return the exit code and release associated resources // once the command exits. func (c *Cmd) Start() error { + if c.Path == "" && c.lookPathErr == nil { + c.lookPathErr = errors.New("exec: no command") + } if c.lookPathErr != nil { c.closeDescriptors(c.closeAfterStart) c.closeDescriptors(c.closeAfterWait) diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 73aa35f1ae..1913ae81c8 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -1081,3 +1081,11 @@ func TestChildCriticalEnv(t *testing.T) { t.Error("no SYSTEMROOT found") } } + +func TestNoPath(t *testing.T) { + err := new(exec.Cmd).Start() + want := "exec: no command" + if err == nil || err.Error() != want { + t.Errorf("new(Cmd).Start() = %v, want %q", err, want) + } +} |