diff options
author | Robert Griesemer <gri@golang.org> | 2010-06-09 15:19:22 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-06-09 15:19:22 -0700 |
commit | a5d659d8a1a54b60f4ab974dd7b32dc01ea8da36 (patch) | |
tree | 611c192af8aa99185f5c02285be804984a43978b /src/pkg | |
parent | a7a0e2b02e5c5bb30484a5bcfaa9826c5b62c850 (diff) | |
download | golang-a5d659d8a1a54b60f4ab974dd7b32dc01ea8da36.tar.gz |
go/scanner: report illegal escape sequences
R=golang-dev, r
CC=golang-dev
http://codereview.appspot.com/1636043
Diffstat (limited to 'src/pkg')
-rw-r--r-- | src/pkg/go/scanner/scanner.go | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/src/pkg/go/scanner/scanner.go b/src/pkg/go/scanner/scanner.go index 576b95a28..e5ac9d772 100644 --- a/src/pkg/go/scanner/scanner.go +++ b/src/pkg/go/scanner/scanner.go @@ -345,34 +345,43 @@ exit: } -func (S *Scanner) scanDigits(base, length int) { - for length > 0 && digitVal(S.ch) < base { - S.next() - length-- - } - if length > 0 { - S.error(S.pos, "illegal char escape") - } -} - - func (S *Scanner) scanEscape(quote int) { pos := S.pos - ch := S.ch - S.next() - switch ch { + + var i, base, max uint32 + switch S.ch { case 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', quote: - // nothing to do + S.next() + return case '0', '1', '2', '3', '4', '5', '6', '7': - S.scanDigits(8, 3-1) // 1 char read already + i, base, max = 3, 8, 255 case 'x': - S.scanDigits(16, 2) + S.next() + i, base, max = 2, 16, 255 case 'u': - S.scanDigits(16, 4) + S.next() + i, base, max = 4, 16, unicode.MaxRune case 'U': - S.scanDigits(16, 8) + S.next() + i, base, max = 8, 16, unicode.MaxRune default: - S.error(pos, "illegal char escape") + S.next() // always make progress + S.error(pos, "unknown escape sequence") + return + } + + var x uint32 + for ; i > 0; i-- { + d := uint32(digitVal(S.ch)) + if d > base { + S.error(S.pos, "illegal character in escape sequence") + return + } + x = x*base + d + S.next() + } + if x > max || 0xd800 <= x && x < 0xe000 { + S.error(pos, "escape sequence is invalid Unicode code point") } } |