checker: fix hex literal overflow check (#10782)

pull/10804/head
shadowninja55 2021-07-14 15:33:02 -04:00 committed by GitHub
parent 646c1e15e2
commit de6918b8c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 11 deletions

View File

@ -493,6 +493,8 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Typ
return ast.string_type return ast.string_type
} }
const hex_lit_overflow_message = 'hex character literal overflows string'
pub fn (mut c Checker) string_lit(mut node ast.StringLiteral) ast.Type { pub fn (mut c Checker) string_lit(mut node ast.StringLiteral) ast.Type {
mut idx := 0 mut idx := 0
for idx < node.val.len { for idx < node.val.len {
@ -511,12 +513,24 @@ pub fn (mut c Checker) string_lit(mut node ast.StringLiteral) ast.Type {
mut hex_char_count := 0 mut hex_char_count := 0
for ch.is_hex_digit() { for ch.is_hex_digit() {
hex_char_count++ hex_char_count++
if hex_char_count > 4 {
end_pos := token.Position{ end_pos := token.Position{
...start_pos ...start_pos
len: idx + 1 - start_idx len: idx + 1 - start_idx
} }
c.error('hex character literal overflows string', end_pos) match hex_char_count {
1...5 {}
6 {
first_digit := node.val[idx - 5] - 48
second_digit := node.val[idx - 4] - 48
if first_digit > 1 {
c.error(checker.hex_lit_overflow_message, end_pos)
} else if first_digit == 1 && second_digit > 0 {
c.error(checker.hex_lit_overflow_message, end_pos)
}
}
else {
c.error(checker.hex_lit_overflow_message, end_pos)
}
} }
idx++ idx++
ch = node.val[idx] or { return ast.string_type } ch = node.val[idx] or { return ast.string_type }

View File

@ -1,4 +1,18 @@
vlib/v/checker/tests/hex_literal_overflow.vv:1:35: error: hex character literal overflows string vlib/v/checker/tests/hex_literal_overflow.vv:1:7: error: hex character literal overflows string
1 | s := 'this hex character literal, \xffffffff, should be caught by the checker' 1 | a := '\x11ffff'
| ~~~~~~~ | ~~~~~~~~
2 | println(s) 2 | b := '\x20ffff'
3 | c := '\x10fffff'
vlib/v/checker/tests/hex_literal_overflow.vv:2:7: error: hex character literal overflows string
1 | a := '\x11ffff'
2 | b := '\x20ffff'
| ~~~~~~~~
3 | c := '\x10fffff'
4 | println(a)
vlib/v/checker/tests/hex_literal_overflow.vv:3:7: error: hex character literal overflows string
1 | a := '\x11ffff'
2 | b := '\x20ffff'
3 | c := '\x10fffff'
| ~~~~~~~~~
4 | println(a)
5 | println(b)

View File

@ -1,2 +1,6 @@
s := 'this hex character literal, \xffffffff, should be caught by the checker' a := '\x11ffff'
println(s) b := '\x20ffff'
c := '\x10fffff'
println(a)
println(b)
println(c)