From 99574e465dc88f1320b9839c7c29da7c0e419b6c Mon Sep 17 00:00:00 2001 From: Henrixounez <30901439+Henrixounez@users.noreply.github.com> Date: Fri, 18 Sep 2020 01:02:06 +0200 Subject: [PATCH] scanner: allow escape on null character (#6404) --- vlib/v/checker/tests/string_char_null_err.out | 5 +++++ vlib/v/checker/tests/string_char_null_err.vv | 3 +++ vlib/v/scanner/scanner.v | 5 +++-- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/string_char_null_err.out create mode 100644 vlib/v/checker/tests/string_char_null_err.vv diff --git a/vlib/v/checker/tests/string_char_null_err.out b/vlib/v/checker/tests/string_char_null_err.out new file mode 100644 index 0000000000..942bfd1f99 --- /dev/null +++ b/vlib/v/checker/tests/string_char_null_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/string_char_null_err.vv:2:31: error: 0 character in a string literal + 1 | fn main() { + 2 | println('Null character: \0') + | ^ + 3 | } diff --git a/vlib/v/checker/tests/string_char_null_err.vv b/vlib/v/checker/tests/string_char_null_err.vv new file mode 100644 index 0000000000..d60854f829 --- /dev/null +++ b/vlib/v/checker/tests/string_char_null_err.vv @@ -0,0 +1,3 @@ +fn main() { + println('Null character: \0') +} diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 5e31e6a094..02ba60d33c 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1216,14 +1216,15 @@ fn (mut s Scanner) ident_string() string { } // Don't allow \0 if c == `0` && s.pos > 2 && s.text[s.pos - 1] == slash { - if s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit() { + if (s.pos < s.text.len - 1 && s.text[s.pos + 1].is_digit()) || s.count_symbol_before(s.pos - 1, slash) % 2 == 0 { } else if !is_cstr { s.error('0 character in a string literal') } } // Don't allow \x00 if c == `0` && s.pos > 5 && s.expect('\\x0', s.pos - 3) { - if !is_cstr { + if s.count_symbol_before(s.pos - 3, slash) % 2 == 0 { + } else if !is_cstr { s.error('0 character in a string literal') } }