From 5fc4604aa8b593d8d9e93c98a1380114633b7c6a Mon Sep 17 00:00:00 2001 From: Tim Cooper Date: Fri, 26 Oct 2018 16:34:27 -0500 Subject: bytes, strings: fix Reader.UnreadRune returning without error on a zero Reader Fixes #28269 Change-Id: I878dff43c0b6bdb98702d8e73f2ecd984fb2350f Reviewed-on: https://go-review.googlesource.com/c/145098 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/strings/reader.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/strings/reader.go') diff --git a/src/strings/reader.go b/src/strings/reader.go index 6c1a5064c0..eb2fa1164c 100644 --- a/src/strings/reader.go +++ b/src/strings/reader.go @@ -13,6 +13,7 @@ import ( // A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, // io.ByteScanner, and io.RuneScanner interfaces by reading // from a string. +// The zero value for Reader operates like a Reader of an empty string. type Reader struct { s string i int64 // current reading index @@ -70,10 +71,10 @@ func (r *Reader) ReadByte() (byte, error) { } func (r *Reader) UnreadByte() error { - r.prevRune = -1 if r.i <= 0 { return errors.New("strings.Reader.UnreadByte: at beginning of string") } + r.prevRune = -1 r.i-- return nil } @@ -94,6 +95,9 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) { } func (r *Reader) UnreadRune() error { + if r.i <= 0 { + return errors.New("strings.Reader.UnreadRune: at beginning of string") + } if r.prevRune < 0 { return errors.New("strings.Reader.UnreadRune: previous operation was not ReadRune") } -- cgit v1.3-5-g45d5