]> Cypherpunks repositories - gostls13.git/commitdiff
unicode: panic if given rune is negative in SimpleFold
authorHiroshi Ioka <hirochachacha@gmail.com>
Wed, 12 Oct 2016 14:05:35 +0000 (23:05 +0900)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 12 Oct 2016 15:45:03 +0000 (15:45 +0000)
Fixes #16690

Change-Id: I6db588c4b0f23c5ec6bc9b85a488b60fab3f2f13
Reviewed-on: https://go-review.googlesource.com/30892
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/unicode/letter.go
src/unicode/letter_test.go

index 8aec920d22f83af835c724f1d322fd394bf7f9ed..462daf88b93018e7cebe36addaa5faaf75adbb01 100644 (file)
@@ -332,6 +332,10 @@ type foldPair struct {
 //     SimpleFold('1') = '1'
 //
 func SimpleFold(r rune) rune {
+       if r < 0 {
+               panic("unicode: negative rune is disallowed")
+       }
+
        if int(r) < len(asciiFold) {
                return rune(asciiFold[r])
        }
index 0eb9ee95b08eac10d28513784a6b2b02fa13e2a8..15e4ade2a32b42685f1c0c8b86f732438e2a2981 100644 (file)
@@ -434,6 +434,20 @@ func TestSimpleFold(t *testing.T) {
        }
 }
 
+func TestSimpleFoldPanic(t *testing.T) {
+       got := func() (r interface{}) {
+               defer func() { r = recover() }()
+               SimpleFold(-1)
+               return nil
+       }()
+       want := "unicode: negative rune is disallowed"
+
+       s, _ := got.(string)
+       if s != want {
+               t.Errorf("SimpleFold(-1) should panic, got: %q, want: %q", got, want)
+       }
+}
+
 // Running 'go test -calibrate' runs the calibration to find a plausible
 // cutoff point for linear search of a range list vs. binary search.
 // We create a fake table and then time how long it takes to do a