From ed9a01dda4d67f463d248a8f30d19ef3f3f54ac9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 15 Apr 2026 05:58:20 +0300 Subject: [PATCH] cgen: fix `bad address` error when using `os.execve()` (fixes #26007) --- vlib/v/gen/c/struct.v | 7 +++- ...struct_init_result_propagation.c.must_have | 2 + .../heap_struct_init_result_propagation.vv | 39 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 vlib/v/gen/c/testdata/heap_struct_init_result_propagation.c.must_have create mode 100644 vlib/v/gen/c/testdata/heap_struct_init_result_propagation.vv diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 06dacfc04..6351128b4 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -462,7 +462,7 @@ fn (mut g Gen) struct_init(node ast.StructInit) { } } -fn (g &Gen) can_use_direct_heap_struct_init(node ast.StructInit, sym ast.TypeSymbol, aligned int, const_msvc_init bool) bool { +fn (mut g Gen) can_use_direct_heap_struct_init(node ast.StructInit, sym ast.TypeSymbol, aligned int, const_msvc_init bool) bool { if g.is_shared || g.inside_cast_in_heap > 0 || g.inside_cinit || g.inside_const || g.inside_global_decl || aligned != 0 || const_msvc_init || node.typ.has_flag(.option) || node.has_update_expr || sym.kind != .struct { @@ -476,6 +476,11 @@ fn (g &Gen) can_use_direct_heap_struct_init(node ast.StructInit, sym ast.TypeSym || node.init_fields.len != info.fields.len { return false } + for init_field in node.init_fields { + if g.need_tmp_var_in_expr(init_field.expr) { + return false + } + } if node.no_keys { return true } diff --git a/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.c.must_have b/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.c.must_have new file mode 100644 index 000000000..378cf325a --- /dev/null +++ b/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.c.must_have @@ -0,0 +1,2 @@ +c->process = (HEAP(main__Process, ((main__Process){.path = (*(string*) +,.argv = c->args,.dir = c->dir,}))); diff --git a/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.vv b/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.vv new file mode 100644 index 000000000..1460d7bd7 --- /dev/null +++ b/vlib/v/gen/c/testdata/heap_struct_init_result_propagation.vv @@ -0,0 +1,39 @@ +// vtest vflags: -prod +import os + +struct Process { + path string + argv []string + dir string +} + +struct Command { + path string + args []string + dir string +mut: + process &Process = unsafe { nil } +} + +fn lookup(cmd string) !string { + return os.find_abs_path_of_executable(cmd)! +} + +fn build(mut c Command) ! { + if !isnil(c.process) { + return error('reuse') + } + c.process = &Process{ + path: lookup(c.path)! + argv: c.args + dir: c.dir + } +} + +fn main() { + mut cmd := Command{ + path: 'bash' + args: ['-c', 'echo ok'] + } + build(mut cmd) or { panic(err) } +} -- 2.39.5