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

bufio: squelch error from Peek len(b) = n #3022

Closed
nwaples opened this issue Feb 14, 2012 · 5 comments
Closed

bufio: squelch error from Peek len(b) = n #3022

nwaples opened this issue Feb 14, 2012 · 5 comments
Milestone

Comments

@nwaples
Copy link
Contributor

nwaples commented Feb 14, 2012

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull", "hg update default", rebuild, and
retry
what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?

package main

import (
    "io"
    "fmt"
    "bufio"
)

type testReader struct {}

func (r *testReader) Read(p []byte) (int, error) {
    return copy(p, "abcd"), io.EOF
}

func main() {
    buf := bufio.NewReaderSize(new(testReader), 32)
    s, err := buf.Peek(2)
    fmt.Println(string(s),err)
}


What is the expected output?
ab <nil>

What do you see instead?
ab EOF

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
linux

Which revision are you using?  (hg identify)
e01251826b3a tip

Please provide any additional information below.

The last if clause in the Peek function will always be false.

In the case that an error is returned, should it use
b.err instead or b.readErr()? Otherwise it will eat the 
error and it wont be available for next Read call.
@robpike
Copy link
Contributor

robpike commented Feb 15, 2012

Comment 1:

See the documentation for io.Reader. Check the count before the error.

Status changed to WorkingAsIntended.

@nwaples
Copy link
Contributor Author

nwaples commented Feb 15, 2012

Comment 2:

ok. I assumed it would be consistent with read, in that
it would only return an error once you reached the end of
the buffer.
 Doing a Peek that doesnt reach end of buffer and getting
and EOF error may give the wrong impression that there is
nothing left to read.

@rsc
Copy link
Contributor

rsc commented Feb 15, 2012

Comment 3:

At least as documented, Peek should not be returning that error.
It's not directly analogous to Read, in that Peek always tries to
get the number of bytes requested (it can't be short without an error).
I'm inclined to fix the code to match the comment.

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

Owner changed to builder@golang.org.

Status changed to Accepted.

@bradfitz
Copy link
Contributor

Comment 4:

http://golang.org/cl/5674060

@bradfitz
Copy link
Contributor

Comment 5:

This issue was closed by revision 88f8af1.

Status changed to Fixed.

@rsc rsc added this to the Go1 milestone Apr 10, 2015
@rsc rsc removed the priority-go1 label Apr 10, 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
Development

No branches or pull requests

5 participants