Skip to content

Commit

Permalink
os/exec: not add a suffix to Cmd.Path
Browse files Browse the repository at this point in the history
For avoid data race for cmd.Path , in CL 527820 fixed data race , but addition of suffixe as shown in golang#66586 was also introduced.
The result of call lookExtensions is actually the name passed to os.StartProcess,
But solutions at the time chose to reuse cmd.Path to represent the name passed to os.StartProcess,since this results in golang#66586,
So use new field save call lookExtensions result.

Fixes golang#66586

Change-Id: Ib1baa6d7803f9471af6e38bcb55f0298422e6743
  • Loading branch information
qiulaidongfeng committed Apr 25, 2024
1 parent 8960925 commit 78e0ae5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/os/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ type Cmd struct {
// See https://go.dev/blog/path-security
// and https://go.dev/issue/43724 for more context.
lookPathErr error

// cacheLookExtensions cache the result of calling lookExtensions,
// use it only on windows.
cacheLookExtensions string
}

// A ctxResult reports the result of watching the Context associated with a
Expand Down Expand Up @@ -437,9 +441,7 @@ func Command(name string, arg ...string) *Cmd {
// cmd.Dir may be set after we return from this function and that may cause
// the command to resolve to a different extension.
lp, err := lookExtensions(name, "")
if lp != "" {
cmd.Path = lp
}
cmd.cacheLookExtensions = lp
if err != nil {
cmd.Err = err
}
Expand Down Expand Up @@ -640,8 +642,8 @@ func (c *Cmd) Start() error {
}
return c.Err
}
lp := c.Path
if runtime.GOOS == "windows" && !filepath.IsAbs(c.Path) {
lp := c.cacheLookExtensions
if lp == "" && runtime.GOOS == "windows" {
// If c.Path is relative, we had to wait until now
// to resolve it in case c.Dir was changed.
// (If it is absolute, we already resolved its extension in Command
Expand Down

0 comments on commit 78e0ae5

Please sign in to comment.