os: don't let File.Readdir return an empty slice and nil error

In the case of a file being deleted while Readdir was running, it was
possible for File.Readdir to return an empty slice and a nil error,
counter to its documentation.

Fixes #16919

Change-Id: If0e42882eea52fbf5530317a1895f3829ea8e67b
Reviewed-on: https://go-review.googlesource.com/28056
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2016-08-29 21:27:40 +00:00
parent 11e3955e10
commit 9f8335b7e7
2 changed files with 44 additions and 0 deletions

View file

@ -34,6 +34,11 @@ func (f *File) readdir(n int) (fi []FileInfo, err error) {
}
fi = append(fi, fip)
}
if len(fi) == 0 && err == nil && n > 0 {
// Per File.Readir, the slice must be non-empty or err
// must be non-nil if n > 0.
err = io.EOF
}
return fi, err
}