Skip to content

bufio: Scanner and zero length reads #5268

@nwaples

Description

@nwaples
running go1.1beta2.linux-amd64

 Just a few issues regarding bufio Scanner and zero length reads of the underlying io.Reader.

1. It is not documented that the scanner also stops on a zero length read.

2. If the underlying reader returns a zero length read with an error that is 
   not io.EOF the error is masked and Err() returns nil.
    Eg, this program will not print out the error.

package main

import (
    "bufio"
    "errors"
    "fmt"
)

type errReader struct{}

func (t *errReader) Read(p []byte) (int, error) {
    return 0, errors.New("Random Err")
}

func main() {
    scanner := bufio.NewScanner(new(errReader))
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        fmt.Println(err)
    }
}


3. I don't see in any of the other standard library readers documentation
   that they guarantee no zero length reads. If I am being paranoid, can
   I safely use them without the scan prematurely terminating before EOF?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions