Skip to content

Commit

Permalink
[release-branch.go1.18] os/exec: return clear error for missing cmd.Path
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Roland Shoemaker <[email protected]>
(cherry picked from commit 960ffa9)
Reviewed-on: https://go-review.googlesource.com/c/go/+/408577
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Roland Shoemaker <[email protected]>
  • Loading branch information
rsc authored and toothrot committed May 27, 2022
1 parent a08baaa commit 6c65a4a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/os/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/os/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6c65a4a

Please sign in to comment.