Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: inconsistent behaviour of File's methods on a nil pointer #5824

Closed
gopherbot opened this issue Jul 1, 2013 · 5 comments
Closed

os: inconsistent behaviour of File's methods on a nil pointer #5824

gopherbot opened this issue Jul 1, 2013 · 5 comments
Milestone

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Jul 1, 2013

by robryk:

Some of *os.File's methods return an error when called on a nil pointer. For example,
the following do so: Read, ReadAt, Write, WriteAt. These methods explicitely check for a
nil pointer.

Some other panic when they are called on a nil pointer. For instance: Name, Close, Seek.

The documentation doesn't say anything about this case. It might be nice to do the same
thing for all methods, but I'm not sure if the compatibility promises don't prevent any
change there.

The behaviour was mentioned in:
https://plus.google.com/u/0/104862366695495126200/posts/LE5tfxKaUS4
@robpike

This comment has been minimized.

Copy link
Contributor

@robpike robpike commented Jul 1, 2013

Comment 1:

Labels changed: added priority-later, removed priority-triage.

Status changed to Accepted.

@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Jul 30, 2013

Comment 2:

Labels changed: added go1.2maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

@rsc rsc commented Jul 30, 2013

Comment 3:

Labels changed: added feature.

@robpike

This comment has been minimized.

Copy link
Contributor

@robpike robpike commented Aug 8, 2013

Comment 4:

The change can only be made for methods that return an error, so Fd and Name are not
candidates.
The following program prints:
Chdir panicked
Chmod panicked
Chown panicked
Close panicked
Readdir panicked
Readdirnames panicked
Seek panicked
Stat panicked
Truncate panicked
package main
import (
    "fmt"
    "os"
)
func test(name string, f func()){
    defer func() {
        if recover() != nil {
            fmt.Println(name, "panicked")
        }
    }()
    f()
}
func main() {
    p := make([]byte, 10)
    test("Chdir", func(){(*os.File)(nil).Chdir()})
    test("Chmod", func(){(*os.File)(nil).Chmod(0)})
    test("Chown", func(){(*os.File)(nil).Chown(0, 0)})
    test("Close", func(){(*os.File)(nil).Close()})
    test("Read", func(){(*os.File)(nil).Read(p)})
    test("ReadAt", func(){(*os.File)(nil).ReadAt(p, 0)})
    test("Readdir", func(){(*os.File)(nil).Readdir(0)})
    test("Readdirnames", func(){(*os.File)(nil).Readdirnames(0)})
    test("Seek", func(){(*os.File)(nil).Seek(0, 0)})
    test("Stat", func(){(*os.File)(nil).Stat()})
    test("Sync", func(){(*os.File)(nil).Sync()})
    test("Truncate", func(){(*os.File)(nil).Truncate(0)})
    test("Write", func(){(*os.File)(nil).Write(p)})
    test("WriteAt", func(){(*os.File)(nil).WriteAt(p, 0)})
    test("WriteString", func(){(*os.File)(nil).WriteString("hi")})
}
@robpike

This comment has been minimized.

Copy link
Contributor

@robpike robpike commented Aug 20, 2013

Comment 5:

This issue was closed by revision 4cb086b.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.