From: Brendan Hansen Date: Tue, 14 Mar 2023 13:48:58 +0000 (-0500) Subject: bugfix: some `#cstr`s did not have null bytes at the end X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3678c4a20e3c12f43c63810ec91b722b00eefbfd;p=onyx.git bugfix: some `#cstr`s did not have null bytes at the end --- diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 1fa59527..8aaac08b 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -4263,25 +4263,26 @@ static void emit_string_literal(OnyxWasmModule* mod, AstStrLit* strlit) { if (index != -1) { StrLitInfo sti = mod->string_literals[index].value; strlit->data_id = sti.data_id; - strlit->length = sti.len; + strlit->length = sti.len + (strlit->is_cstr ? 1 : 0); bh_free(global_heap_allocator, strdata); return; } // :ProperLinking - u32 actual_length = length + (strlit->is_cstr ? 1 : 0); + // The length used here is one greater than the string length, because + // we DO want to include the null-terminator in the outputted string. WasmDatum datum = { .alignment = 1, - .length = actual_length, + .length = length + 1, .data = strdata, }; strlit->data_id = emit_data_entry(mod, &datum); - strlit->length = length; + strlit->length = length + (strlit->is_cstr ? 1 : 0); // :ProperLinking - shput(mod->string_literals, (char *) strdata, ((StrLitInfo) { strlit->data_id, strlit->length })); + shput(mod->string_literals, (char *) strdata, ((StrLitInfo) { strlit->data_id, length })); } static u32 emit_data_entry(OnyxWasmModule *mod, WasmDatum *datum) {