Skip to content

os/exec: exec.LookPath return errors when PATH starts with semicolon (parsed as contains empty string at first) and working directory is same as the target executable on windows #69636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ray1422 opened this issue Sep 26, 2024 · 3 comments

Comments

@ray1422
Copy link

ray1422 commented Sep 26, 2024

Go version

go version go1.20.14 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Administrator\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.14
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\Administrator\AppData\Local\Temp\go-build3123780178=/tmp/go-build -gno-record-gcc-switches

What did you do?

I have a file my.bat in C:\, and my program

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	os.Setenv("path", `;C:\`)
	s, err := exec.LookPath("my.bat")
	fmt.Printf("%s %v\n", s, err)
}

What did you see happen?

when this program executed with working dir C:\, it prints

my.bat exec: "my.bat": cannot run executable found relative to current directory

but when I execute it with other working dir, it parsed normally and printed

C:\my.bat <nil>

I think it's caused by dotErr is returned too early, although the executable do exist in the PATH.

What did you expect to see?

C:\ is in the path, it should print

C:\my.bat <nil>

no matter what working directory is it.

@ray1422 ray1422 changed the title os/exec: exec.LookPath return errors when PATH starts with semicolon (parsed as contains empty string at first) and working directory is same as the target executable os/exec: exec.LookPath return errors when PATH starts with semicolon (parsed as contains empty string at first) and working directory is same as the target executable on windows Sep 26, 2024
@seankhliao
Copy link
Member

from the package docs:

To avoid those security problems, as of Go 1.19, this package will not resolve a program using an implicit or explicit path entry relative to the current directory. That is, if you run LookPath("go"), it will not successfully return ./go on Unix nor .\go.exe on Windows, no matter how the path is configured. Instead, if the usual path algorithms would result in that answer, these functions return an error err satisfying errors.Is(err, ErrDot).

closing as working as intended.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2024
@ray1422
Copy link
Author

ray1422 commented Sep 27, 2024

It returns normally when removing the leading semicolon of the PATH.

If this is intended, all binary that registered as Windows services, with PWD C:\Windows\sys32, will be unable to call some commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants