diff options
| author | Bryan C. Mills <bcmills@google.com> | 2021-09-23 11:56:16 -0400 |
|---|---|---|
| committer | Bryan C. Mills <bcmills@google.com> | 2021-10-01 17:40:49 +0000 |
| commit | 243d65c8e551be424008a3dfcaf5c87dc1f35a77 (patch) | |
| tree | 1e4018c75253b2dc7f171f3f85d92db3e997cea4 /src/bufio/bufio_test.go | |
| parent | 33576247e295b19311484d943d85495006d2fb39 (diff) | |
| download | go-243d65c8e551be424008a3dfcaf5c87dc1f35a77.tar.xz | |
bufio: reject UnreadByte or UnreadRune after a Discard or WriteTo
Discard is not really a read operation, and in theory it could
Seek the underlying Reader without actually reading anything,
so an UnreadByte following a Discard is disallowed.
Similarly, although WriteTo usually does end up calling Read on the
underlying buffer, if the underlying Reader implements io.WriterTo it
may instead terminate in a call to WriteTo, without ever buffering or
even seeing the last byte written. (It is conceptually read-like, but
not strictly “a read operation”.)
Fixes #48446
Change-Id: Ide6f2b157332b423486810399f66140c914144e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/351810
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Diffstat (limited to 'src/bufio/bufio_test.go')
| -rw-r--r-- | src/bufio/bufio_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/bufio/bufio_test.go b/src/bufio/bufio_test.go index 04a810c206..8e8a8a1778 100644 --- a/src/bufio/bufio_test.go +++ b/src/bufio/bufio_test.go @@ -304,6 +304,40 @@ func TestNoUnreadByteAfterPeek(t *testing.T) { } } +func TestNoUnreadRuneAfterDiscard(t *testing.T) { + br := NewReader(strings.NewReader("example")) + br.ReadRune() + br.Discard(1) + if err := br.UnreadRune(); err == nil { + t.Error("UnreadRune didn't fail after Discard") + } +} + +func TestNoUnreadByteAfterDiscard(t *testing.T) { + br := NewReader(strings.NewReader("example")) + br.ReadByte() + br.Discard(1) + if err := br.UnreadByte(); err == nil { + t.Error("UnreadByte didn't fail after Discard") + } +} + +func TestNoUnreadRuneAfterWriteTo(t *testing.T) { + br := NewReader(strings.NewReader("example")) + br.WriteTo(io.Discard) + if err := br.UnreadRune(); err == nil { + t.Error("UnreadRune didn't fail after WriteTo") + } +} + +func TestNoUnreadByteAfterWriteTo(t *testing.T) { + br := NewReader(strings.NewReader("example")) + br.WriteTo(io.Discard) + if err := br.UnreadByte(); err == nil { + t.Error("UnreadByte didn't fail after WriteTo") + } +} + func TestUnreadByte(t *testing.T) { segments := []string{"Hello, ", "world"} r := NewReader(&StringReader{data: segments}) |
