Skip to content

Commit 2b7492e

Browse files
authored
cgen: fix empty shared struct init (fix #16234) (#26354)
1 parent 19e79c4 commit 2b7492e

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎vlib/v/gen/c/struct.v‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
473473
field_name := if sym.language == .v { c_name(field.name) } else { field.name }
474474
if sym.info is ast.Struct {
475475
if sym.info.fields.len == 0 {
476+
// For empty struct fields, check if it's a shared field
477+
if field.typ.has_flag(.shared_f) {
478+
g.write('.${field_name} = ')
479+
g.init_shared_field(field)
480+
return true
481+
}
476482
return false
477483
} else if !field.has_default_expr {
478484
mut has_option_field := false

‎vlib/v/tests/concurrency/shared_elem_test.v‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,21 @@ fn test_array_of_shared() {
168168
assert e == 3
169169
assert f == -17
170170
}
171+
172+
// Test for issue #16234: empty shared struct initialized as null causing segmentation fault
173+
struct EmptyStruct {}
174+
175+
struct OwnerWithEmptyShared {
176+
empty EmptyStruct
177+
lck shared EmptyStruct
178+
}
179+
180+
fn test_shared_empty_struct() {
181+
mut i := OwnerWithEmptyShared{}
182+
dump(i)
183+
// Test that we can lock the empty shared struct without segfault
184+
lock i.lck {
185+
// Empty struct, nothing to do
186+
}
187+
assert true
188+
}

0 commit comments

Comments
 (0)