- Strings assigned to longer arrays would crash codegen, e.g. char[10] x = "abcd.

This commit is contained in:
Christoffer Lerno
2025-12-12 20:35:11 +01:00
parent bf62d11a73
commit 475816251b
3 changed files with 26 additions and 3 deletions

View File

@@ -4821,9 +4821,6 @@ static void llvm_emit_const_expr(GenContext *c, BEValue *be_value, Expr *expr)
ArraySize size = expr->const_expr.bytes.len;
size++;
if (is_array && type->array.len > size) size = type->array.len;
LLVMValueRef global_name = llvm_add_global_raw(c, is_bytes ? ".bytes" : ".str", LLVMArrayType(llvm_get_type(c, type_char), size), 1);
llvm_set_private_declaration(global_name);
LLVMSetGlobalConstant(global_name, 1);
LLVMValueRef data = llvm_get_zstring(c, expr->const_expr.bytes.ptr, expr->const_expr.bytes.len);
LLVMValueRef trailing_zeros = NULL;
if (size > len + 1)
@@ -4835,6 +4832,10 @@ static void llvm_emit_const_expr(GenContext *c, BEValue *be_value, Expr *expr)
LLVMValueRef values[2] = { data, trailing_zeros };
data = llvm_get_packed_struct(c, values, 2);
}
LLVMValueRef global_name = llvm_add_global_raw(c, is_bytes ? ".bytes" : ".str", LLVMTypeOf(data), 1);
llvm_set_private_declaration(global_name);
LLVMSetGlobalConstant(global_name, 1);
LLVMSetInitializer(global_name, data);
if (is_array)
{