os: be consistent about File methods with nil receivers

Some crashed, some didn't. Make a nil receiver always
return ErrInvalid rather than crash.
Fixes #5824.
The program in the bug listing is silent now, at least on my Mac.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/13108044
This commit is contained in:
Rob Pike 2013-08-20 14:33:03 +10:00
parent a3695fb227
commit 4cb086b838
7 changed files with 49 additions and 1 deletions

View file

@ -96,6 +96,9 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
// Close closes the File, rendering it unusable for I/O.
// It returns an error, if any.
func (f *File) Close() error {
if f == nil {
return ErrInvalid
}
return f.file.close()
}
@ -117,6 +120,9 @@ func (file *file) close() error {
// Stat returns the FileInfo structure describing file.
// If there is an error, it will be of type *PathError.
func (f *File) Stat() (fi FileInfo, err error) {
if f == nil {
return nil, ErrInvalid
}
var stat syscall.Stat_t
err = syscall.Fstat(f.fd, &stat)
if err != nil {