Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
g.write('\t\t')
}
is_iface_or_sumtype := elem_sym.kind in [.sum_type, .interface]
is_iface := elem_sym.kind == .interface
if is_iface {
g.inside_cast_in_heap++
}
for i, expr in node.exprs {
expr_type := if node.expr_types.len > i { node.expr_types[i] } else { node.elem_type }
if expr_type == ast.string_type
Expand Down Expand Up @@ -91,6 +95,9 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
}
}
}
if is_iface {
g.inside_cast_in_heap--
}
g.write('}))')
if g.is_shared {
g.write('}, sizeof(${shared_styp}))')
Expand Down
10 changes: 9 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3143,10 +3143,18 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty
false
}

// When casting an lvalue to an interface inside an array context
// (e.g. variadic args, array init), the interface stores a pointer
// that may outlive the source variable. Heap-allocate to prevent
// dangling pointers. (fixes #26760)
is_interface_in_heap_context := fname.contains('_to_Interface_')
&& g.inside_cast_in_heap > 0

if !is_cast_fixed_array_init && (is_comptime_variant || !expr.is_lvalue()
|| (expr is ast.Ident && (expr.obj.is_simple_define_const()
|| (expr.obj is ast.Var && expr.obj.is_index_var)))
|| is_primitive_to_interface || is_fn_arg) {
|| is_primitive_to_interface || is_fn_arg
|| is_interface_in_heap_context) {
// Note: the `_to_sumtype_` family of functions do call memdup internally, making
// another duplicate with the HEAP macro is redundant, so use ADDR instead:
if expr.is_as_cast() {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,11 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
g.writeln('${g.styp(varg_type)} ${tmp_var};')
g.write('builtin___option_ok((${base_type}[]) {')
}
elem_sym := g.table.sym(arr_info.elem_type)
is_iface := elem_sym.kind == .interface
if is_iface {
g.inside_cast_in_heap++
}
g.write('builtin__new_array_from_c_array${noscan}(${variadic_count}, ${variadic_count}, sizeof(${elem_type}), _MOV((${elem_type}[${variadic_count}]){')
for j in arg_nr .. args.len {
g.ref_or_deref_arg(args[j], arr_info.elem_type, node.language,
Expand All @@ -2871,6 +2876,9 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
}
}
g.write('}))')
if is_iface {
g.inside_cast_in_heap--
}
if is_option {
g.writeln(' }, (${option_name}*)&${tmp_var}, sizeof(${base_type}));')
g.write(tmp)
Expand Down
148 changes: 148 additions & 0 deletions vlib/v/tests/fns/variadic_interface_chain_forwarding_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import arrays

// Regression test for https://github.com/vlang/v/issues/26760
// Interface values passed through variadic parameter chains must preserve
// their type tags and data pointers.

interface Value {}

fn process_params(params []Value) string {
mut result := []string{}
for i := 0; i < params.len; i++ {
param := params[i]
match param {
string {
result << 'string:${param}'
}
i32 {
result << 'i32:${param}'
}
[]u8 {
result << '[]u8:${param}'
}
else {
result << 'unknown'
}
}
}
return result.join(', ')
}

struct Statement {
mut:
handle int
}

struct Transaction {
mut:
handle int
}

fn (mut stmt Statement) execute(params ...Value) !string {
return process_params(params)
}

fn (stmt Statement) close() ! {}

fn (mut t Transaction) prepare(query string) !Statement {
return Statement{
handle: 1
}
}

fn (mut t Transaction) execute(query string, params ...Value) !string {
mut stmt := t.prepare(query)!
result := stmt.execute(...params)!
stmt.close()!
return result
}

struct ListParams {
email ?string
role ?string
offset i32
fetch i32
}

fn get_conditions(p ListParams) (string, []Value) {
mut conditions := []string{}
mut params := []Value{}

if email := p.email {
conditions = arrays.concat(conditions, 'email = ?')
params = arrays.concat(params, email)
}

if role := p.role {
conditions = arrays.concat(conditions, 'role = ?')
params = arrays.concat(params, role)
}

return conditions.join(' AND '), params
}

fn test_variadic_interface_forwarding_with_match() ! {
p := ListParams{
email: 'info@peony.com'
offset: 0
fetch: 10
}

conditions, mut params := get_conditions(p)
params = arrays.concat(params, p.offset, p.fetch)

mut tx := Transaction{
handle: 1
}
result := tx.execute('SELECT * FROM users WHERE ${conditions}', ...params)!
assert result == 'string:info@peony.com, i32:0, i32:10', 'got: ${result}'
}

fn test_variadic_interface_forwarding_single_param() ! {
p := ListParams{
email: 'info@peony.com'
offset: 0
fetch: 10
}

conditions, params := get_conditions(p)

mut tx := Transaction{
handle: 1
}
result := tx.execute('SELECT COUNT(*) FROM users WHERE ${conditions}', ...params)!
assert result == 'string:info@peony.com', 'got: ${result}'
}

fn test_variadic_interface_forwarding_with_byte_arrays() ! {
user_id_bin := [u8(1), 2, 3, 4]
email := 'info@peony.com'

mut params := [Value(user_id_bin), email]

mut tx := Transaction{
handle: 1
}
result := tx.execute('INSERT INTO users', ...params)!
assert result == '[]u8:&[1, 2, 3, 4], string:info@peony.com', 'got: ${result}'
}

fn test_variadic_interface_forwarding_repeated() ! {
for _ in 0 .. 100 {
p := ListParams{
email: 'info@peony.com'
role: 'admin'
offset: 5
fetch: 20
}

conditions, mut params := get_conditions(p)
params = arrays.concat(params, p.offset, p.fetch)

mut tx := Transaction{
handle: 1
}
result := tx.execute('SELECT * FROM users WHERE ${conditions}', ...params)!
assert result == 'string:info@peony.com, string:admin, i32:5, i32:20', 'got: ${result}'
}
}
Loading