hcl/scanner: handle \"\\0 input properly

This commit is contained in:
Mitchell Hashimoto 2016-06-21 13:38:25 -07:00
parent 685b5f7416
commit a6c7514d8d
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 17 additions and 3 deletions

View File

@ -525,16 +525,25 @@ func (s *Scanner) scanEscape() rune {
// scanDigits scans a rune with the given base for n times. For example an
// octal notation \184 would yield in scanDigits(ch, 8, 3)
func (s *Scanner) scanDigits(ch rune, base, n int) rune {
start := n
for n > 0 && digitVal(ch) < base {
ch = s.next()
if ch == eof {
break
}
n--
}
if n > 0 {
s.err("illegal char escape")
}
// we scanned all digits, put the last non digit char back
s.unread()
if n != start {
// we scanned all digits, put the last non digit char back,
// only if we read anything at all
s.unread()
}
return ch
}

View File

@ -283,6 +283,11 @@ func TestPosition(t *testing.T) {
}
}
func TestNullChar(t *testing.T) {
s := New([]byte("\"\\0"))
s.Scan() // Used to panic
}
func TestComment(t *testing.T) {
testTokenList(t, tokenLists["comment"])
}
@ -378,7 +383,7 @@ func TestRealExample(t *testing.T) {
Main interface
EOF
}
network_interface {
device_index = 1
description = <<-EOF