1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:04:40 -07:00

go/scanner: don't accept '\x0g' character escape (bug fix)

Added more test cases.

R=rsc
CC=golang-dev
https://golang.org/cl/2804041
This commit is contained in:
Robert Griesemer 2010-11-01 12:50:21 -07:00
parent 6dc4c58f87
commit 68ac0c1d94
2 changed files with 19 additions and 5 deletions

View File

@ -368,15 +368,19 @@ func (S *Scanner) scanEscape(quote int) {
} }
var x uint32 var x uint32
for ; i > 0; i-- { for ; i > 0 && S.ch != quote && S.ch >= 0; i-- {
d := uint32(digitVal(S.ch)) d := uint32(digitVal(S.ch))
if d > base { if d >= base {
S.error(S.pos, "illegal character in escape sequence") S.error(S.pos, "illegal character in escape sequence")
return break
} }
x = x*base + d x = x*base + d
S.next() S.next()
} }
// in case of an error, consume remaining chars
for ; i > 0 && S.ch != quote && S.ch >= 0; i-- {
S.next()
}
if x > max || 0xd800 <= x && x < 0xe000 { if x > max || 0xd800 <= x && x < 0xe000 {
S.error(pos, "escape sequence is invalid Unicode code point") S.error(pos, "escape sequence is invalid Unicode code point")
} }

View File

@ -610,8 +610,18 @@ var errors = []struct {
pos int pos int
err string err string
}{ }{
{"\"\"", token.STRING, 0, ""}, {`#`, token.ILLEGAL, 0, "illegal character '#' (U+23)"},
{"\"", token.STRING, 0, "string not terminated"}, {`' '`, token.CHAR, 0, ""},
{`''`, token.CHAR, 0, "illegal character literal"},
{`'\8'`, token.CHAR, 2, "unknown escape sequence"},
{`'\08'`, token.CHAR, 3, "illegal character in escape sequence"},
{`'\x0g'`, token.CHAR, 4, "illegal character in escape sequence"},
{`'\Uffffffff'`, token.CHAR, 2, "escape sequence is invalid Unicode code point"},
{`'`, token.CHAR, 0, "character literal not terminated"},
{`""`, token.STRING, 0, ""},
{`"`, token.STRING, 0, "string not terminated"},
{"``", token.STRING, 0, ""},
{"`", token.STRING, 0, "string not terminated"},
{"/**/", token.COMMENT, 0, ""}, {"/**/", token.COMMENT, 0, ""},
{"/*", token.COMMENT, 0, "comment not terminated"}, {"/*", token.COMMENT, 0, "comment not terminated"},
{"//\n", token.COMMENT, 0, ""}, {"//\n", token.COMMENT, 0, ""},