]> Cypherpunks repositories - gostls13.git/commitdiff
go/scanner: don't accept '\x0g' character escape (bug fix)
authorRobert Griesemer <gri@golang.org>
Mon, 1 Nov 2010 19:50:21 +0000 (12:50 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 1 Nov 2010 19:50:21 +0000 (12:50 -0700)
Added more test cases.

R=rsc
CC=golang-dev
https://golang.org/cl/2804041

src/pkg/go/scanner/scanner.go
src/pkg/go/scanner/scanner_test.go

index f38c0252c3d6323e12f7f52ad2fcc3d14749f4a5..663636c46ee61fc3969967d6186f16e64497deff 100644 (file)
@@ -368,15 +368,19 @@ func (S *Scanner) scanEscape(quote int) {
        }
 
        var x uint32
-       for ; i > 0; i-- {
+       for ; i > 0 && S.ch != quote && S.ch >= 0; i-- {
                d := uint32(digitVal(S.ch))
-               if d > base {
+               if d >= base {
                        S.error(S.pos, "illegal character in escape sequence")
-                       return
+                       break
                }
                x = x*base + d
                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 {
                S.error(pos, "escape sequence is invalid Unicode code point")
        }
index 480502e3fbb55d304d60af7e5462942a62d8b681..794b191e8383bb66b145d3497b98c0cef948f503 100644 (file)
@@ -610,8 +610,18 @@ var errors = []struct {
        pos int
        err string
 }{
-       {"\"\"", token.STRING, 0, ""},
-       {"\"", token.STRING, 0, "string not terminated"},
+       {`#`, token.ILLEGAL, 0, "illegal character '#' (U+23)"},
+       {`' '`, 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, "comment not terminated"},
        {"//\n", token.COMMENT, 0, ""},