From 4853c51770f5e99d5d690801e5cb963848591587 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 1 Nov 2011 00:17:05 -0400 Subject: [PATCH] os: do not interpret 0-length read as EOF Fixes #2402. R=golang-dev, bradfitz, r CC=golang-dev https://golang.org/cl/5298081 --- src/pkg/os/file.go | 2 +- src/pkg/os/os_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/pkg/os/file.go b/src/pkg/os/file.go index 4335d45e5a0..9f982e183a2 100644 --- a/src/pkg/os/file.go +++ b/src/pkg/os/file.go @@ -69,7 +69,7 @@ func (file *File) Read(b []byte) (n int, err Error) { if n < 0 { n = 0 } - if n == 0 && !iserror(e) { + if n == 0 && len(b) > 0 && !iserror(e) { return 0, EOF } if iserror(e) { diff --git a/src/pkg/os/os_test.go b/src/pkg/os/os_test.go index f9bacebf113..1547ce0dbf1 100644 --- a/src/pkg/os/os_test.go +++ b/src/pkg/os/os_test.go @@ -165,6 +165,27 @@ func TestLstat(t *testing.T) { } } +// Read with length 0 should not return EOF. +func TestRead0(t *testing.T) { + path := sfdir + "/" + sfname + f, err := Open(path) + if err != nil { + t.Fatal("open failed:", err) + } + defer f.Close() + + b := make([]byte, 0) + n, err := f.Read(b) + if n != 0 || err != nil { + t.Errorf("Read(0) = %d, %v, want 0, nil", n, err) + } + b = make([]byte, 100) + n, err = f.Read(b) + if n <= 0 || err != nil { + t.Errorf("Read(100) = %d, %v, want >0, nil", n, err) + } +} + func testReaddirnames(dir string, contents []string, t *testing.T) { file, err := Open(dir) defer file.Close()