Skip to content

Commit

Permalink
properly handle unix read on directory
Browse files Browse the repository at this point in the history
  • Loading branch information
noboruma authored and deadprogram committed Dec 19, 2024
1 parent 6507765 commit eeba90f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/os/file_anyos.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !baremetal && !js && !wasm_unknown && !nintendoswitch

// Portions copyright 2009 The Go Authors. All rights reserved.
// Portions copyright 2009-2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -97,6 +97,11 @@ type unixFileHandle uintptr
// read and any error encountered. At end of file, Read returns 0, io.EOF.
func (f unixFileHandle) Read(b []byte) (n int, err error) {
n, err = syscall.Read(syscallFd(f), b)
// In case of EISDIR, n == -1.
// This breaks the assumption that n always represent the number of read bytes.
if err == syscall.EISDIR {
n = 0
}
err = handleSyscallError(err)
if n == 0 && len(b) > 0 && err == nil {
err = io.EOF
Expand Down
19 changes: 19 additions & 0 deletions src/os/file_anyos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,22 @@ func TestClose(t *testing.T) {
}
}
}

func TestReadOnDir(t *testing.T) {
name := TempDir() + "/_os_test_TestReadOnDir"
defer Remove(name)
f, err := OpenFile(name, O_RDWR|O_CREATE, 0644)
if err != nil {
t.Errorf("OpenFile %s: %s", name, err)
return
}
var buf [32]byte
n, err := f.Read(buf[:])
if err == nil {
t.Errorf("Error expected")
return
}
if n != 0 {
t.Errorf("Wrong read bytes: %s", err)
}
}

0 comments on commit eeba90f

Please sign in to comment.