{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/9657","labels_url":"https://api.github.com/repos/golang/go/issues/9657/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/9657/comments","events_url":"https://api.github.com/repos/golang/go/issues/9657/events","html_url":"https://github.com/golang/go/issues/9657","id":55110028,"number":9657,"title":"bufio: Writer.Flush panics if it holds a nil interface writer","user":{"login":"stevvooe","id":120601,"avatar_url":"https://avatars.githubusercontent.com/u/120601?v=3","gravatar_id":"","url":"https://api.github.com/users/stevvooe","html_url":"https://github.com/stevvooe","followers_url":"https://api.github.com/users/stevvooe/followers","following_url":"https://api.github.com/users/stevvooe/following{/other_user}","gists_url":"https://api.github.com/users/stevvooe/gists{/gist_id}","starred_url":"https://api.github.com/users/stevvooe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevvooe/subscriptions","organizations_url":"https://api.github.com/users/stevvooe/orgs","repos_url":"https://api.github.com/users/stevvooe/repos","events_url":"https://api.github.com/users/stevvooe/events{/privacy}","received_events_url":"https://api.github.com/users/stevvooe/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":14,"created_at":"2015-01-22T03:22:54Z","updated_at":"2015-01-22T06:09:15Z","closed_at":"2015-01-22T03:30:11Z","body":"Calling `(*bufio.Writer).Flush` panics if `(*bufio.Writer).Reset` is called with a `nil` `io.Writer`, then `(*bufio.Writer).Write` is called before the flush. This seems like an unlikely scenario but it has been seen on docker/docker#10228. Though the use case may be invalid, the call should not panic.\r\n\r\nThe following code reproduces the issue:\r\n\r\n```go\r\npackage main\r\n\r\nimport (\r\n\t\"bufio\"\r\n\t\"bytes\"\r\n)\r\n\r\nfunc main() {\r\n\tvar buf bytes.Buffer\r\n\twr := bufio.NewWriter(&buf)\r\n\twr.Reset(nil)\r\n\twr.Write([]byte(\"asdf\"))\r\n\twr.Flush()\r\n}\r\n```\r\n\r\nRunning the above results in the following panic:\r\n\r\n```\r\npanic: runtime error: invalid memory address or nil pointer dereference\r\n[signal 0xb code=0x1 addr=0x20 pc=0x3366a]\r\n\r\ngoroutine 1 [running]:\r\nbufio.(*Writer).flush(0x2080f0000, 0x0, 0x0)\r\n\t/usr/local/go/src/bufio/bufio.go:530 +0xda\r\nbufio.(*Writer).Flush(0x2080f0000, 0x0, 0x0)\r\n\t/usr/local/go/src/bufio/bufio.go:519 +0x3a\r\nmain.main()\r\n\t/tmp/bug.go:13 +0x10b\r\n\r\ngoroutine 2 [runnable]:\r\nruntime.forcegchelper()\r\n\t/usr/local/go/src/runtime/proc.go:90\r\nruntime.goexit()\r\n\t/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1\r\n\r\ngoroutine 3 [runnable]:\r\nruntime.bgsweep()\r\n\t/usr/local/go/src/runtime/mgc0.go:82\r\nruntime.goexit()\r\n\t/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1\r\nexit status 2\r\n```\r\n\r\nThis was reproduced with Go 1.4.1 on Mac OS X 10.9 but has been experienced in other versions."},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/70975973","html_url":"https://github.com/golang/go/issues/9657#issuecomment-70975973","issue_url":"https://api.github.com/repos/golang/go/issues/9657","id":70975973,"user":{"login":"bradfitz","id":2621,"avatar_url":"https://avatars.githubusercontent.com/u/2621?v=3","gravatar_id":"","url":"https://api.github.com/users/bradfitz","html_url":"https://github.com/bradfitz","followers_url":"https://api.github.com/users/bradfitz/followers","following_url":"https://api.github.com/users/bradfitz/following{/other_user}","gists_url":"https://api.github.com/users/bradfitz/gists{/gist_id}","starred_url":"https://api.github.com/users/bradfitz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradfitz/subscriptions","organizations_url":"https://api.github.com/users/bradfitz/orgs","repos_url":"https://api.github.com/users/bradfitz/repos","events_url":"https://api.github.com/users/bradfitz/events{/privacy}","received_events_url":"https://api.github.com/users/bradfitz/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T06:09:15Z","updated_at":"2015-01-22T06:09:15Z","body":"I have to agree that this is all working as intended. @minux and @griesemer mentioned all the most important points: Reset(nil) must remain as supported to allow people to free a Writer without freeing the buffer of a pooled bufio.Writer. It's the callers mistake to _use_ that bufio.Writer before Resetting it back to something valid. "}}