diff options
author | David Benjamin <davidben@google.com> | 2020-12-25 12:02:04 -0500 |
---|---|---|
committer | Emmanuel Odeke <emmanuel@orijtech.com> | 2021-02-24 04:01:25 +0000 |
commit | 43652dc46f770253b3603f47165b1568b439b0b5 (patch) | |
tree | 3af97107b0f79ad061f31d8dac51ef96b5ac16ae /src/strings | |
parent | 37805292550e7144200b09320ffb61f21d421f8d (diff) | |
download | go-43652dc46f770253b3603f47165b1568b439b0b5.tar.gz go-43652dc46f770253b3603f47165b1568b439b0b5.zip |
bufio, bytes, strings: handle negative runes in WriteRune
Updates #43254
Change-Id: I7d4bf3b99cc36ca2156af5bb01a1c595419d1d3c
Reviewed-on: https://go-review.googlesource.com/c/go/+/280492
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Rob Pike <r@golang.org>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/strings')
-rw-r--r-- | src/strings/builder.go | 3 | ||||
-rw-r--r-- | src/strings/builder_test.go | 11 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/strings/builder.go b/src/strings/builder.go index 6ff151d74b..547e52e84d 100644 --- a/src/strings/builder.go +++ b/src/strings/builder.go @@ -103,7 +103,8 @@ func (b *Builder) WriteByte(c byte) error { // It returns the length of r and a nil error. func (b *Builder) WriteRune(r rune) (int, error) { b.copyCheck() - if r < utf8.RuneSelf { + // Compare as uint32 to correctly handle negative runes. + if uint32(r) < utf8.RuneSelf { b.buf = append(b.buf, byte(r)) return 1, nil } diff --git a/src/strings/builder_test.go b/src/strings/builder_test.go index b662efe7a5..e3d239266f 100644 --- a/src/strings/builder_test.go +++ b/src/strings/builder_test.go @@ -8,6 +8,7 @@ import ( "bytes" . "strings" "testing" + "unicode/utf8" ) func check(t *testing.T, b *Builder, want string) { @@ -301,6 +302,16 @@ func TestBuilderCopyPanic(t *testing.T) { } } +func TestBuilderWriteInvalidRune(t *testing.T) { + // Invalid runes, including negative ones, should be written as + // utf8.RuneError. + for _, r := range []rune{-1, utf8.MaxRune + 1} { + var b Builder + b.WriteRune(r) + check(t, &b, "\uFFFD") + } +} + var someBytes = []byte("some bytes sdljlk jsklj3lkjlk djlkjw") var sinkS string |