Skip to content

bufio: Writer.Flush panics if it holds a nil interface writer #9657

@stevvooe

Description

@stevvooe

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 moby/moby#10228. Though the use case may be invalid, the call should not panic.

The following code reproduces the issue:

package main

import (
    "bufio"
    "bytes"
)

func main() {
    var buf bytes.Buffer
    wr := bufio.NewWriter(&buf)
    wr.Reset(nil)
    wr.Write([]byte("asdf"))
    wr.Flush()
}

Running the above results in the following panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x3366a]

goroutine 1 [running]:
bufio.(*Writer).flush(0x2080f0000, 0x0, 0x0)
    /usr/local/go/src/bufio/bufio.go:530 +0xda
bufio.(*Writer).Flush(0x2080f0000, 0x0, 0x0)
    /usr/local/go/src/bufio/bufio.go:519 +0x3a
main.main()
    /tmp/bug.go:13 +0x10b

goroutine 2 [runnable]:
runtime.forcegchelper()
    /usr/local/go/src/runtime/proc.go:90
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

This was reproduced with Go 1.4.1 on Mac OS X 10.9 but has been experienced in other versions.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions