Skip to content
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

os: ReadFile on directories does not return EISDIR in windows #43322

Open
dop251 opened this issue Dec 22, 2020 · 3 comments · May be fixed by #43334
Open

os: ReadFile on directories does not return EISDIR in windows #43322

dop251 opened this issue Dec 22, 2020 · 3 comments · May be fixed by #43334

Comments

@dop251
Copy link

@dop251 dop251 commented Dec 22, 2020

What version of Go are you using (go version)?

$ go version
go version 1.15.6 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\vagrant\AppData\Local\go-build
set GOENV=C:\Users\vagrant\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\vagrant\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\vagrant\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\vagrant\go\wintest\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\vagrant\AppData\Local\Temp\go-build505615858=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://play.golang.org/p/GwvDhx3PqK-

What did you expect to see?

All good

What did you see instead?

Unexpected error: read /: The handle is invalid

@egonelbre
Copy link
Contributor

@egonelbre egonelbre commented Dec 22, 2020

Internally it ends up doing code that is equivalent to:

#include <windows.h>
#include <stdio.h>

void main() {
	WIN32_FIND_DATA data;
	HANDLE h = FindFirstFileA("\\", &data);

	char buf[100];
	BOOL r = ReadFile(h, &buf[0], 100, 0, 0);
	DWORD err = GetLastError();
	printf("%d\n", err);
}

Which does return the invalid handle error.

@dop251
Copy link
Author

@dop251 dop251 commented Dec 22, 2020

I believe it should be fixed. Opening a directory in read/write mode does produce the correct error because there is a special handling for it:

r, errd := openDir(name)

What's missing is returning of the same error when a Read() is attempted on a directory file.

@ianlancetaylor ianlancetaylor changed the title ioutil.ReadFile() on directories does not return EISDIR in windows os: ReadFile on directories does not return EISDIR in windows Dec 22, 2020
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Dec 22, 2020
imxyb added a commit to imxyb/go that referenced this issue Dec 23, 2020
@imxyb imxyb linked a pull request that will close this issue Dec 23, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 23, 2020

Change https://golang.org/cl/279852 mentions this issue: os: fix openFileNolog when open dir in windows

imxyb added a commit to imxyb/go that referenced this issue Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants