From 78e0ae522804cc51b4912423cb11dcaa27bd2aa7 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Thu, 25 Apr 2024 20:28:42 +0800 Subject: [PATCH] os/exec: not add a suffix to Cmd.Path For avoid data race for cmd.Path , in CL 527820 fixed data race , but addition of suffixe as shown in #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 #66586, So use new field save call lookExtensions result. Fixes #66586 Change-Id: Ib1baa6d7803f9471af6e38bcb55f0298422e6743 --- src/os/exec/exec.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/os/exec/exec.go b/src/os/exec/exec.go index 35e4e7e792fdbc..070495ccce0120 100644 --- a/src/os/exec/exec.go +++ b/src/os/exec/exec.go @@ -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 @@ -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 } @@ -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