From 2eb11110d68ada5381c20e56d25b9ae03d38afd0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 4 Aug 2021 08:39:20 +0800 Subject: [PATCH] cgen: fix autofree error of array init with string variable (fix #10427) (#11041) --- vlib/v/gen/c/array.v | 9 ++++++++- .../tests/valgrind/array_init_with_string_variable.v | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/valgrind/array_init_with_string_variable.v diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 9e31f4d3be..6be4cdfcd1 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -110,7 +110,14 @@ fn (mut g Gen) array_init(node ast.ArrayInit) { g.write('\t\t') } for i, expr in node.exprs { - g.expr_with_cast(expr, node.expr_types[i], node.elem_type) + if node.expr_types[i] == ast.string_type && expr !is ast.StringLiteral + && expr !is ast.StringInterLiteral { + g.write('string_clone(') + g.expr(expr) + g.write(')') + } else { + g.expr_with_cast(expr, node.expr_types[i], node.elem_type) + } if i != len - 1 { if i > 0 && i & 7 == 0 { // i > 0 && i % 8 == 0 g.writeln(',') diff --git a/vlib/v/tests/valgrind/array_init_with_string_variable.v b/vlib/v/tests/valgrind/array_init_with_string_variable.v new file mode 100644 index 0000000000..8c8f4f3fd8 --- /dev/null +++ b/vlib/v/tests/valgrind/array_init_with_string_variable.v @@ -0,0 +1,11 @@ +module main + +fn main() { + a := 'Hello, ' + b := 'World!' + c := a + b + + d := [a, b, c] + + println(d.len) +}