aboutsummaryrefslogtreecommitdiff
path: root/src/internal/execabs/execabs.go
diff options
context:
space:
mode:
authorBryan Mills <bcmills@google.com>2022-04-29 21:38:40 +0000
committerGopher Robot <gobot@golang.org>2022-04-29 23:04:17 +0000
commitf2b674756b3b684118e4245627d4ed8c07e518e7 (patch)
tree424683d102e4992d35a557a7f03df9637fb0f32c /src/internal/execabs/execabs.go
parent2278a51fa0570b01f58a80d60589fcf8e33be9c8 (diff)
downloadgo-f2b674756b3b684118e4245627d4ed8c07e518e7.tar.xz
Revert "os/exec: return error when PATH lookup would use current directory"
This reverts CL 381374. Reason for revert: broke tests for x/sys/execabs. Updates #43724. Updates #43947. Change-Id: I9eb3adb5728dead66dbd20f6afe1e7a77e2a26f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/403058 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/internal/execabs/execabs.go')
-rw-r--r--src/internal/execabs/execabs.go40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/internal/execabs/execabs.go b/src/internal/execabs/execabs.go
index 5f60fbb119..9a05d971da 100644
--- a/src/internal/execabs/execabs.go
+++ b/src/internal/execabs/execabs.go
@@ -12,7 +12,11 @@ package execabs
import (
"context"
+ "fmt"
"os/exec"
+ "path/filepath"
+ "reflect"
+ "unsafe"
)
var ErrNotFound = exec.ErrNotFound
@@ -23,14 +27,44 @@ 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) {
- return exec.LookPath(file)
+ 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 = ""
+ }
}
func CommandContext(ctx context.Context, name string, arg ...string) *exec.Cmd {
- return exec.CommandContext(ctx, name, arg...)
+ cmd := exec.CommandContext(ctx, name, arg...)
+ fixCmd(name, cmd)
+ return cmd
+
}
func Command(name string, arg ...string) *exec.Cmd {
- return exec.Command(name, arg...)
+ cmd := exec.Command(name, arg...)
+ fixCmd(name, cmd)
+ return cmd
}