diff --git a/vlib/v/checker/tests/string_escape_x_err_a.out b/vlib/v/checker/tests/string_escape_x_err_a.out new file mode 100644 index 0000000000..f441565e7e --- /dev/null +++ b/vlib/v/checker/tests/string_escape_x_err_a.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/string_escape_x_err_a.vv:2:15: error: `\x` used with no following hex digits + 1 | fn main() { + 2 | println('\x') + | ^ + 3 | } diff --git a/vlib/v/checker/tests/string_escape_x_err_a.vv b/vlib/v/checker/tests/string_escape_x_err_a.vv new file mode 100644 index 0000000000..fdbd44bdeb --- /dev/null +++ b/vlib/v/checker/tests/string_escape_x_err_a.vv @@ -0,0 +1,3 @@ +fn main() { + println('\x') +} diff --git a/vlib/v/checker/tests/string_escape_x_err_b.out b/vlib/v/checker/tests/string_escape_x_err_b.out new file mode 100644 index 0000000000..da90fd10d7 --- /dev/null +++ b/vlib/v/checker/tests/string_escape_x_err_b.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/string_escape_x_err_b.vv:2:15: error: `\x` used with no following hex digits + 1 | fn main() { + 2 | println('\xhh') + | ^ + 3 | } diff --git a/vlib/v/checker/tests/string_escape_x_err_b.vv b/vlib/v/checker/tests/string_escape_x_err_b.vv new file mode 100644 index 0000000000..deabe28899 --- /dev/null +++ b/vlib/v/checker/tests/string_escape_x_err_b.vv @@ -0,0 +1,3 @@ +fn main() { + println('\xhh') +} diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 537d35198c..85ae34c96b 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1039,6 +1039,12 @@ fn (mut s Scanner) ident_string() string { s.error(r'cannot use `\x00` (NULL character) in the string literal') } } + // Escape `\x` + if prevc == slash && + c == `x` && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 && !is_raw && !is_cstr && + (s.text[s.pos + 1] == s.quote || !s.text[s.pos + 1].is_hex_digit()) { + s.error(r'`\x` used with no following hex digits') + } // ${var} (ignore in vfmt mode) (skip \$) if prevc == `$` && c == `{` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { s.is_inside_string = true diff --git a/vlib/v/scanner/scanner_test.v b/vlib/v/scanner/scanner_test.v index 41afe0546d..573f672913 100644 --- a/vlib/v/scanner/scanner_test.v +++ b/vlib/v/scanner/scanner_test.v @@ -136,3 +136,8 @@ fn test_ref_ref_array_ref_ref_foo() { assert result[5] == .amp assert result[6] == .name } + +fn test_escape_string() { + assert '\x61' == 'a' + assert '\x62' == 'b' +}