diff options
| author | Russ Cox <rsc@golang.org> | 2022-04-29 20:05:26 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2022-05-02 14:54:05 +0000 |
| commit | 349cc83389f71c459b7820b0deecdf81221ba46c (patch) | |
| tree | d21420aa8571fb505ab4bb040d289a6603ed0c88 /src/internal/execabs/execabs.go | |
| parent | 32acceb359717e434ceb48681426b377b722d11e (diff) | |
| download | go-349cc83389f71c459b7820b0deecdf81221ba46c.tar.xz | |
os/exec: return error when PATH lookup would use current directory
CL 381374 was reverted because x/sys/execabs broke.
This CL reapplies CL 381374, but adding a lookPathErr error
field back, for execabs to manipulate with reflect.
That field will just be a bit of scar tissue in this package forever,
to keep old code working with new toolchains.
CL 403256 fixes x/sys/execabs's test to be ready for the change.
Older versions of x/sys/execabs will keep working
(that is, will keep rejecting what they should reject),
but they will return a slightly different error from LookPath
without that CL, and the test fails because of the different
error text.
For #43724.
This reverts commit f2b674756b3b684118e4245627d4ed8c07e518e7.
Change-Id: Iee55f8cd9939e1bd31e5cbdada50681cdc505117
Reviewed-on: https://go-review.googlesource.com/c/go/+/403274
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/internal/execabs/execabs.go')
| -rw-r--r-- | src/internal/execabs/execabs.go | 40 |
1 files changed, 3 insertions, 37 deletions
diff --git a/src/internal/execabs/execabs.go b/src/internal/execabs/execabs.go index 9a05d971da..5f60fbb119 100644 --- a/src/internal/execabs/execabs.go +++ b/src/internal/execabs/execabs.go @@ -12,11 +12,7 @@ package execabs import ( "context" - "fmt" "os/exec" - "path/filepath" - "reflect" - "unsafe" ) var ErrNotFound = exec.ErrNotFound @@ -27,44 +23,14 @@ type ( ExitError = exec.ExitError ) -func relError(file, path string) error { - return fmt.Errorf("%s resolves to executable relative to current directory (.%c%s)", file, filepath.Separator, path) -} - func LookPath(file string) (string, error) { - path, err := exec.LookPath(file) - if err != nil { - return "", err - } - if filepath.Base(file) == file && !filepath.IsAbs(path) { - return "", relError(file, path) - } - return path, nil -} - -func fixCmd(name string, cmd *exec.Cmd) { - if filepath.Base(name) == name && !filepath.IsAbs(cmd.Path) { - // exec.Command was called with a bare binary name and - // exec.LookPath returned a path which is not absolute. - // Set cmd.lookPathErr and clear cmd.Path so that it - // cannot be run. - lookPathErr := (*error)(unsafe.Pointer(reflect.ValueOf(cmd).Elem().FieldByName("lookPathErr").Addr().Pointer())) - if *lookPathErr == nil { - *lookPathErr = relError(name, cmd.Path) - } - cmd.Path = "" - } + return exec.LookPath(file) } func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd { - cmd := exec.CommandContext(ctx, name, arg...) - fixCmd(name, cmd) - return cmd - + return exec.CommandContext(ctx, name, arg...) } func Command(name string, arg ...string) *exec.Cmd { - cmd := exec.Command(name, arg...) - fixCmd(name, cmd) - return cmd + return exec.Command(name, arg...) } |
