From: Rui Ueyama Date: Wed, 19 Mar 2014 03:52:58 +0000 (-0700) Subject: bytes: fix panic in Map X-Git-Tag: go1.3beta1~328 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1a21dbc5720326b0e325a54c3e01c0e50b32eb03;p=gostls13.git bytes: fix panic in Map utf8.RuneLen returns -1 for an invalid rune. In that case we need to extend the internal buffer at least by 3 for \uFFFD. Fixes #7577. LGTM=iant R=golang-codereviews, iant CC=golang-codereviews https://golang.org/cl/77420044 --- diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 644bf75b89..0c53e4c0b7 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -356,7 +356,11 @@ func Map(mapping func(r rune) rune, s []byte) []byte { } r = mapping(r) if r >= 0 { - if nbytes+utf8.RuneLen(r) > maxbytes { + rl := utf8.RuneLen(r) + if rl < 0 { + rl = len(string(utf8.RuneError)) + } + if nbytes+rl > maxbytes { // Grow the buffer. maxbytes = maxbytes*2 + utf8.UTFMax nb := make([]byte, maxbytes) diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index 808655a4a4..b16ac9f515 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -785,6 +785,16 @@ func TestMap(t *testing.T) { if string(m) != expect { t.Errorf("drop: expected %q got %q", expect, m) } + + // 6. Invalid rune + invalidRune := func(r rune) rune { + return utf8.MaxRune + 1 + } + m = Map(invalidRune, []byte("x")) + expect = "\uFFFD" + if string(m) != expect { + t.Errorf("invalidRune: expected %q got %q", expect, m) + } } func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }