Skip to content

bufio: Writer.ReadFrom ignores the stored error #35194

@pam4

Description

@pam4

If an error occurs writing to a Writer, no more data will be accepted and all subsequent writes, and Flush, will return the error.

type writer struct{}

func (w writer) Write(p []byte) (n int, err error) {
	return 0, errors.New("bad move")
}

func main() {
	var wr = bufio.NewWriter(writer{})
	wr.WriteString("test1") // 5 <nil>
	wr.Flush()              // bad move
	wr.ReadFrom(strings.NewReader("test2")) // 5 <nil>
	wr.Buffered() // 10
}

https://play.golang.org/p/--WBbDHbIS9

no more data will be accepted

Then I would expect ReadFrom to fail and the Buffered() count not to grow.

all subsequent writes [...] will return the error.

Isn't ReadFrom a write?

This seems unlikely to have bad consequences, but I don't think it's a valid reason for the documentation to be incorrect about corner cases.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions