mirror of
https://github.com/golang/go
synced 2024-11-22 05:44:41 -07:00
bufio: don't return errors from good Peeks
Fixes #3022 R=golang-dev, r CC=golang-dev https://golang.org/cl/5674060
This commit is contained in:
parent
3a317183a1
commit
88f8af127a
@ -106,9 +106,12 @@ func (b *Reader) Peek(n int) ([]byte, error) {
|
|||||||
if m > n {
|
if m > n {
|
||||||
m = n
|
m = n
|
||||||
}
|
}
|
||||||
err := b.readErr()
|
var err error
|
||||||
if m < n && err == nil {
|
if m < n {
|
||||||
err = ErrBufferFull
|
err = b.readErr()
|
||||||
|
if err == nil {
|
||||||
|
err = ErrBufferFull
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return b.buf[b.r : b.r+m], err
|
return b.buf[b.r : b.r+m], err
|
||||||
}
|
}
|
||||||
|
@ -539,6 +539,27 @@ func TestPeek(t *testing.T) {
|
|||||||
if _, err := buf.Peek(1); err != io.EOF {
|
if _, err := buf.Peek(1); err != io.EOF {
|
||||||
t.Fatalf("want EOF got %v", err)
|
t.Fatalf("want EOF got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for issue 3022, not exposing a reader's error on a successful Peek.
|
||||||
|
buf = NewReaderSize(dataAndEOFReader("abcd"), 32)
|
||||||
|
if s, err := buf.Peek(2); string(s) != "ab" || err != nil {
|
||||||
|
t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err)
|
||||||
|
}
|
||||||
|
if s, err := buf.Peek(4); string(s) != "abcd" || err != nil {
|
||||||
|
t.Errorf(`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err)
|
||||||
|
}
|
||||||
|
if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil {
|
||||||
|
t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err)
|
||||||
|
}
|
||||||
|
if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF {
|
||||||
|
t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dataAndEOFReader string
|
||||||
|
|
||||||
|
func (r dataAndEOFReader) Read(p []byte) (int, error) {
|
||||||
|
return copy(p, r), io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPeekThenUnreadRune(t *testing.T) {
|
func TestPeekThenUnreadRune(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user