Commit ace14d01 authored by Robert Griesemer's avatar Robert Griesemer

text/scanner: report illegal hexadecimal numbers (bug fix)

R=r
CC=golang-dev
https://golang.org/cl/6450136
parent 2ab18f69
...@@ -389,15 +389,20 @@ func (s *Scanner) scanNumber(ch rune) (rune, rune) { ...@@ -389,15 +389,20 @@ func (s *Scanner) scanNumber(ch rune) (rune, rune) {
if ch == 'x' || ch == 'X' { if ch == 'x' || ch == 'X' {
// hexadecimal int // hexadecimal int
ch = s.next() ch = s.next()
hasMantissa := false
for digitVal(ch) < 16 { for digitVal(ch) < 16 {
ch = s.next() ch = s.next()
hasMantissa = true
}
if !hasMantissa {
s.error("illegal hexadecimal number")
} }
} else { } else {
// octal int or float // octal int or float
seenDecimalDigit := false has8or9 := false
for isDecimal(ch) { for isDecimal(ch) {
if ch > '7' { if ch > '7' {
seenDecimalDigit = true has8or9 = true
} }
ch = s.next() ch = s.next()
} }
...@@ -408,7 +413,7 @@ func (s *Scanner) scanNumber(ch rune) (rune, rune) { ...@@ -408,7 +413,7 @@ func (s *Scanner) scanNumber(ch rune) (rune, rune) {
return Float, ch return Float, ch
} }
// octal int // octal int
if seenDecimalDigit { if has8or9 {
s.error("illegal octal number") s.error("illegal octal number")
} }
} }
......
...@@ -446,6 +446,9 @@ func TestError(t *testing.T) { ...@@ -446,6 +446,9 @@ func TestError(t *testing.T) {
testError(t, `"\'"`, "1:3", "illegal char escape", String) testError(t, `"\'"`, "1:3", "illegal char escape", String)
testError(t, `01238`, "1:6", "illegal octal number", Int) testError(t, `01238`, "1:6", "illegal octal number", Int)
testError(t, `01238123`, "1:9", "illegal octal number", Int)
testError(t, `0x`, "1:3", "illegal hexadecimal number", Int)
testError(t, `0xg`, "1:3", "illegal hexadecimal number", Int)
testError(t, `'aa'`, "1:4", "illegal char literal", Char) testError(t, `'aa'`, "1:4", "illegal char literal", Char)
testError(t, `'`, "1:2", "literal not terminated", Char) testError(t, `'`, "1:2", "literal not terminated", Char)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment