Skip to content

Commit

Permalink
cgen: fix string interp with zero characters (fix #20199) (#20200)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 authored Dec 17, 2023
1 parent ae16878 commit 5abeb41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vlib/v/gen/c/str_intp.v
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write(' str_intp(${node.vals.len}, ')
g.write('_MOV((StrIntpData[]){')
for i, val in node.vals {
escaped_val := util.smart_quote(val, false)
mut escaped_val := util.smart_quote(val, false)
escaped_val = escaped_val.replace('\0', '\\0')

if escaped_val.len > 0 {
g.write('{_SLIT("${escaped_val}"), ')
Expand Down
9 changes: 9 additions & 0 deletions vlib/v/tests/string_interpolation_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ fn test_intp_pointer_specifier_p() {
str2 := '${&foo:p}'
assert str1 == str2
}

// for issue: 20199
// string contains the zero character: `\0`
fn test_contains_zero_characters() {
mut str := 'abc'
str = '${str}\x00${str}'
assert str.len == 7
assert str == 'abc\0abc'
}

0 comments on commit 5abeb41

Please sign in to comment.