-
Notifications
You must be signed in to change notification settings - Fork 17.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os/exec: return error when PATH lookup would use current directory
Following discussion on #43724, change os/exec to take the approach of golang.org/x/sys/execabs, refusing to respect path entries mentioning relative paths by default. Code that insists on being able to find executables in relative directories in the path will need to add a couple lines to override the error. See the updated package docs in exec.go for more details. Fixes #43724. Fixes #43947. Change-Id: I73c1214f322b60b4167a23e956e933d50470fe13 Reviewed-on: https://go-review.googlesource.com/c/go/+/381374 Reviewed-by: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Bryan Mills <bcmills@google.com>
- Loading branch information
Showing
13 changed files
with
248 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pkg os/exec, type Cmd struct, Err error #43724 | ||
pkg os/exec, var ErrDot error #43724 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package exec_test | ||
|
||
import ( | ||
"errors" | ||
"internal/testenv" | ||
"io/ioutil" | ||
"os" | ||
. "os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestLookPath(t *testing.T) { | ||
testenv.MustHaveExec(t) | ||
|
||
tmpDir := filepath.Join(t.TempDir(), "testdir") | ||
if err := os.Mkdir(tmpDir, 0777); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
executable := "execabs-test" | ||
if runtime.GOOS == "windows" { | ||
executable += ".exe" | ||
} | ||
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil { | ||
t.Fatal(err) | ||
} | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := os.Chdir(cwd); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
if err = os.Chdir(tmpDir); err != nil { | ||
t.Fatal(err) | ||
} | ||
origPath := os.Getenv("PATH") | ||
defer os.Setenv("PATH", origPath) | ||
|
||
// Add "." to PATH so that exec.LookPath looks in the current directory on all systems. | ||
// And try to trick it with "../testdir" too. | ||
for _, dir := range []string{".", "../testdir"} { | ||
os.Setenv("PATH", dir+string(filepath.ListSeparator)+origPath) | ||
t.Run("PATH="+dir, func(t *testing.T) { | ||
good := dir + "/execabs-test" | ||
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { | ||
t.Fatalf("LookPath(%q) = %q, %v, want \"%s...\", nil", good, found, err, good) | ||
} | ||
if runtime.GOOS == "windows" { | ||
good = dir + `\execabs-test` | ||
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) { | ||
t.Fatalf("LookPath(%q) = %q, %v, want \"%s...\", nil", good, found, err, good) | ||
} | ||
} | ||
|
||
if _, err := LookPath("execabs-test"); err == nil { | ||
t.Fatalf("LookPath didn't fail when finding a non-relative path") | ||
} else if !errors.Is(err, ErrDot) { | ||
t.Fatalf("LookPath returned unexpected error: want Is ErrDot, got %q", err) | ||
} | ||
|
||
cmd := Command("execabs-test") | ||
if cmd.Err == nil { | ||
t.Fatalf("Command didn't fail when finding a non-relative path") | ||
} else if !errors.Is(cmd.Err, ErrDot) { | ||
t.Fatalf("Command returned unexpected error: want Is ErrDot, got %q", cmd.Err) | ||
} | ||
cmd.Err = nil | ||
|
||
// Clearing cmd.Err should let the execution proceed, | ||
// and it should fail because it's not a valid binary. | ||
if err := cmd.Run(); err == nil { | ||
t.Fatalf("Run did not fail: expected exec error") | ||
} else if errors.Is(err, ErrDot) { | ||
t.Fatalf("Run returned unexpected error ErrDot: want error like ENOEXEC: %q", err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.