From de6918b8c9ea7ed04ff81b65a614c0cf8747eef3 Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Wed, 14 Jul 2021 15:33:02 -0400 Subject: [PATCH] checker: fix hex literal overflow check (#10782) --- vlib/v/checker/check_types.v | 24 +++++++++++++++---- vlib/v/checker/tests/hex_literal_overflow.out | 22 +++++++++++++---- vlib/v/checker/tests/hex_literal_overflow.vv | 8 +++++-- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index c4760704f0..544977b915 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -493,6 +493,8 @@ pub fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Typ 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 { mut idx := 0 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 for ch.is_hex_digit() { hex_char_count++ - if hex_char_count > 4 { - end_pos := token.Position{ - ...start_pos - len: idx + 1 - start_idx + end_pos := token.Position{ + ...start_pos + len: idx + 1 - start_idx + } + 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) } - c.error('hex character literal overflows string', end_pos) } idx++ ch = node.val[idx] or { return ast.string_type } diff --git a/vlib/v/checker/tests/hex_literal_overflow.out b/vlib/v/checker/tests/hex_literal_overflow.out index b6f54ad21f..9475150399 100644 --- a/vlib/v/checker/tests/hex_literal_overflow.out +++ b/vlib/v/checker/tests/hex_literal_overflow.out @@ -1,4 +1,18 @@ -vlib/v/checker/tests/hex_literal_overflow.vv:1:35: error: hex character literal overflows string - 1 | s := 'this hex character literal, \xffffffff, should be caught by the checker' - | ~~~~~~~ - 2 | println(s) +vlib/v/checker/tests/hex_literal_overflow.vv:1:7: error: hex character literal overflows string + 1 | a := '\x11ffff' + | ~~~~~~~~ + 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) diff --git a/vlib/v/checker/tests/hex_literal_overflow.vv b/vlib/v/checker/tests/hex_literal_overflow.vv index 3b1faaaea0..60903f8040 100644 --- a/vlib/v/checker/tests/hex_literal_overflow.vv +++ b/vlib/v/checker/tests/hex_literal_overflow.vv @@ -1,2 +1,6 @@ -s := 'this hex character literal, \xffffffff, should be caught by the checker' -println(s) +a := '\x11ffff' +b := '\x20ffff' +c := '\x10fffff' +println(a) +println(b) +println(c)