Skip to content

Commit

Permalink
os/exec: avoid calling LookPath in cmd.Start for resolved paths
Browse files Browse the repository at this point in the history
Follow up on CL 511458, see https://go-review.googlesource.com/c/go/+/511458/2..4/src/cmd/go/main.go#b270 .

For #36768.

Change-Id: Icc2a4dbb1219b1d69dd10a900478957b0e975847

Change-Id: Icc2a4dbb1219b1d69dd10a900478957b0e975847
GitHub-Last-Rev: bac7e66496806d505270c5b90d53672d80a1ca29
GitHub-Pull-Request: golang/go#61517
Reviewed-on: https://go-review.googlesource.com/c/go/+/512155
Auto-Submit: Bryan Mills <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Run-TryBot: Bryan Mills <[email protected]>
  • Loading branch information
qiulaidongfeng authored and gopherbot committed Jul 26, 2023
1 parent 7fed338 commit 6161935
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
36 changes: 4 additions & 32 deletions src/os/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,32 +590,6 @@ func (c *Cmd) Run() error {
return c.Wait()
}

// lookExtensions finds windows executable by its dir and path.
// It uses LookPath to try appropriate extensions.
// lookExtensions does not search PATH, instead it converts `prog` into `.\prog`.
func lookExtensions(path, dir string) (string, error) {
if filepath.Base(path) == path {
path = "." + string(filepath.Separator) + path
}
if dir == "" {
return LookPath(path)
}
if filepath.VolumeName(path) != "" {
return LookPath(path)
}
if len(path) > 1 && os.IsPathSeparator(path[0]) {
return LookPath(path)
}
dirandpath := filepath.Join(dir, path)
// We assume that LookPath will only add file extension.
lp, err := LookPath(dirandpath)
if err != nil {
return "", err
}
ext := strings.TrimPrefix(lp, dirandpath)
return path + ext, nil
}

// Start starts the specified command but does not wait for it to complete.
//
// If Start returns successfully, the c.Process field will be set.
Expand Down Expand Up @@ -649,13 +623,11 @@ func (c *Cmd) Start() error {
}
return c.Err
}
if runtime.GOOS == "windows" {
lp, err := lookExtensions(c.Path, c.Dir)
if err != nil {
return err
}
c.Path = lp
lp, err := lookExtensions(c.Path, c.Dir)
if err != nil {
return err
}
c.Path = lp
if c.Cancel != nil && c.ctx == nil {
return errors.New("exec: command with a non-nil Cancel was not created with CommandContext")
}
Expand Down
6 changes: 6 additions & 0 deletions src/os/exec/lp_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ func LookPath(file string) (string, error) {
}
return "", &Error{file, ErrNotFound}
}

// lookExtensions is a no-op on non-Windows platforms, since
// they do not restrict executables to specific extensions.
func lookExtensions(path, dir string) (string, error) {
return path, nil
}
6 changes: 6 additions & 0 deletions src/os/exec/lp_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ func LookPath(file string) (string, error) {
}
return "", &Error{file, ErrNotFound}
}

// lookExtensions is a no-op on non-Windows platforms, since
// they do not restrict executables to specific extensions.
func lookExtensions(path, dir string) (string, error) {
return path, nil
}
6 changes: 6 additions & 0 deletions src/os/exec/lp_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ func LookPath(file string) (string, error) {
// Wasm can not execute processes, so act as if there are no executables at all.
return "", &Error{file, ErrNotFound}
}

// lookExtensions is a no-op on non-Windows platforms, since
// they do not restrict executables to specific extensions.
func lookExtensions(path, dir string) (string, error) {
return path, nil
}
43 changes: 43 additions & 0 deletions src/os/exec/lp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ func findExecutable(file string, exts []string) (string, error) {
// As of Go 1.19, LookPath will instead return that path along with an error satisfying
// errors.Is(err, ErrDot). See the package documentation for more details.
func LookPath(file string) (string, error) {
return lookPath(file, pathExt())
}

// lookExtensions finds windows executable by its dir and path.
// It uses LookPath to try appropriate extensions.
// lookExtensions does not search PATH, instead it converts `prog` into `.\prog`.
func lookExtensions(path, dir string) (string, error) {
if filepath.Base(path) == path {
path = "." + string(filepath.Separator) + path
}
exts := pathExt()
if ext := filepath.Ext(path); ext != "" {
for _, e := range exts {
if strings.EqualFold(ext, e) {
// Assume that path has already been resolved.
return path, nil
}
}
}
if dir == "" {
return lookPath(path, exts)
}
if filepath.VolumeName(path) != "" {
return lookPath(path, exts)
}
if len(path) > 1 && os.IsPathSeparator(path[0]) {
return lookPath(path, exts)
}
dirandpath := filepath.Join(dir, path)
// We assume that LookPath will only add file extension.
lp, err := lookPath(dirandpath, exts)
if err != nil {
return "", err
}
ext := strings.TrimPrefix(lp, dirandpath)
return path + ext, nil
}

func pathExt() []string {
var exts []string
x := os.Getenv(`PATHEXT`)
if x != "" {
Expand All @@ -78,7 +117,11 @@ func LookPath(file string) (string, error) {
} else {
exts = []string{".com", ".exe", ".bat", ".cmd"}
}
return exts
}

// lookPath implements LookPath for the given PATHEXT list.
func lookPath(file string, exts []string) (string, error) {
if strings.ContainsAny(file, `:\/`) {
f, err := findExecutable(file, exts)
if err == nil {
Expand Down

0 comments on commit 6161935

Please sign in to comment.