]> Cypherpunks repositories - gostls13.git/commitdiff
os: be consistent about File methods with nil receivers
authorRob Pike <r@golang.org>
Tue, 20 Aug 2013 04:33:03 +0000 (14:33 +1000)
committerRob Pike <r@golang.org>
Tue, 20 Aug 2013 04:33:03 +0000 (14:33 +1000)
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

src/pkg/os/doc.go
src/pkg/os/file.go
src/pkg/os/file_plan9.go
src/pkg/os/file_posix.go
src/pkg/os/file_unix.go
src/pkg/os/file_windows.go
src/pkg/os/stat_windows.go

index c8d0a8632a7b40d0133739e4cab5adb07d50fee1..a954e313d135b78f2ebfc5a19c65c37170cf51f8 100644 (file)
@@ -106,6 +106,9 @@ func Hostname() (name string, err error) {
 // directory, Readdir returns the FileInfo read until that point
 // and a non-nil error.
 func (f *File) Readdir(n int) (fi []FileInfo, err error) {
+       if f == nil {
+               return nil, ErrInvalid
+       }
        return f.readdir(n)
 }
 
@@ -122,5 +125,8 @@ func (f *File) Readdir(n int) (fi []FileInfo, err error) {
 // directory, Readdirnames returns the names read until that point and
 // a non-nil error.
 func (f *File) Readdirnames(n int) (names []string, err error) {
+       if f == nil {
+               return nil, ErrInvalid
+       }
        return f.readdirnames(n)
 }
index 00a8755f423648752ac54c3d0aedd83cb5e47aee..2dd1fcf282f65affadb340af514252c4bfdf79cc 100644 (file)
@@ -174,6 +174,9 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
 // relative to the current offset, and 2 means relative to the end.
 // It returns the new offset and an error, if any.
 func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
+       if f == nil {
+               return 0, ErrInvalid
+       }
        r, e := f.seek(offset, whence)
        if e == nil && f.dirinfo != nil && r != 0 {
                e = syscall.EISDIR
@@ -216,6 +219,9 @@ func Chdir(dir string) error {
 // which must be a directory.
 // If there is an error, it will be of type *PathError.
 func (f *File) Chdir() error {
+       if f == nil {
+               return ErrInvalid
+       }
        if e := syscall.Fchdir(f.fd); e != nil {
                return &PathError{"chdir", f.name, e}
        }
index d6d39a89973570e4ef73d27a619b335dfccd3a34..708163ee1c08dabfa24f1ca867eb84bae34fa3af 100644 (file)
@@ -133,6 +133,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()
 }
 
@@ -156,6 +159,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
+       }
        d, err := dirstat(f)
        if err != nil {
                return nil, err
@@ -167,8 +173,11 @@ func (f *File) Stat() (fi FileInfo, err error) {
 // It does not change the I/O offset.
 // If there is an error, it will be of type *PathError.
 func (f *File) Truncate(size int64) error {
-       var d syscall.Dir
+       if f == nil {
+               return ErrInvalid
+       }
 
+       var d syscall.Dir
        d.Null()
        d.Length = size
 
@@ -188,6 +197,9 @@ const chmodMask = uint32(syscall.DMAPPEND | syscall.DMEXCL | syscall.DMTMP | Mod
 // Chmod changes the mode of the file to mode.
 // If there is an error, it will be of type *PathError.
 func (f *File) Chmod(mode FileMode) error {
+       if f == nil {
+               return ErrInvalid
+       }
        var d syscall.Dir
 
        odir, e := dirstat(f)
@@ -419,6 +431,9 @@ func Lchown(name string, uid, gid int) error {
 // Chown changes the numeric uid and gid of the named file.
 // If there is an error, it will be of type *PathError.
 func (f *File) Chown(uid, gid int) error {
+       if f == nil {
+               return ErrInvalid
+       }
        return &PathError{"chown", f.name, syscall.EPLAN9}
 }
 
index 3df43feaa13c6b73f7e836bec51188c2886a4b10..b64d9edd145cb12c72246c1d9d54c901d287549b 100644 (file)
@@ -86,6 +86,9 @@ func Chmod(name string, mode FileMode) error {
 // Chmod changes the mode of the file to mode.
 // If there is an error, it will be of type *PathError.
 func (f *File) Chmod(mode FileMode) error {
+       if f == nil {
+               return ErrInvalid
+       }
        if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
                return &PathError{"chmod", f.name, e}
        }
@@ -115,6 +118,9 @@ func Lchown(name string, uid, gid int) error {
 // Chown changes the numeric uid and gid of the named file.
 // If there is an error, it will be of type *PathError.
 func (f *File) Chown(uid, gid int) error {
+       if f == nil {
+               return ErrInvalid
+       }
        if e := syscall.Fchown(f.fd, uid, gid); e != nil {
                return &PathError{"chown", f.name, e}
        }
@@ -125,6 +131,9 @@ func (f *File) Chown(uid, gid int) error {
 // It does not change the I/O offset.
 // If there is an error, it will be of type *PathError.
 func (f *File) Truncate(size int64) error {
+       if f == nil {
+               return ErrInvalid
+       }
        if e := syscall.Ftruncate(f.fd, size); e != nil {
                return &PathError{"truncate", f.name, e}
        }
index 855eb50ff59aec5392eaf3f229d5f9572b0e8b00..e0013ac64085d58022ecb9b0de281bf142eb98a0 100644 (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 {
index 7a635661489913acdca4a80ac68df4acd9de3dbb..f6e53d4ff016f9ca6beff9bc25bdff44a81f4ec7 100644 (file)
@@ -153,6 +153,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 (file *File) Close() error {
+       if f == nil {
+               return ErrInvalid
+       }
        return file.file.close()
 }
 
index 8394c2b3206e586383474d723ec27a33e06ed017..6dc386685965c9c70ef8a3fe06304894feae9a26 100644 (file)
@@ -12,6 +12,9 @@ import (
 // Stat returns the FileInfo structure describing file.
 // If there is an error, it will be of type *PathError.
 func (file *File) Stat() (fi FileInfo, err error) {
+       if file == nil {
+               return nil, ErrInvalid
+       }
        if file == nil || file.fd < 0 {
                return nil, syscall.EINVAL
        }