diff --git a/lib/std/core/string.c3 b/lib/std/core/string.c3 index 611d5b800..158aa67a9 100644 --- a/lib/std/core/string.c3 +++ b/lib/std/core/string.c3 @@ -526,6 +526,11 @@ fn String String.tconcat(s1, String s2) => s1.concat(s2, allocator::temp()); fn ZString String.zstr_tcopy(s) => s.zstr_copy(allocator::temp()) @inline; +<* + Copy this string, by duplicating the string, always adding a zero byte + sentinel, so that it safely can be converted to a ZString by a + cast. +*> fn String String.copy(s, Allocator allocator = allocator::heap()) { usz len = s.len; @@ -537,7 +542,7 @@ fn String String.copy(s, Allocator allocator = allocator::heap()) fn void String.free(&s, Allocator allocator = allocator::heap()) { - if (!s.len) return; + if (!s.ptr) return; allocator::free(allocator, s.ptr); *s = ""; } diff --git a/releasenotes.md b/releasenotes.md index 29b3b7265..dce6f3ef4 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -55,6 +55,7 @@ - Assert on certain slice to slice casts. #1768. - Fix vector float -> bool conversion. - Fix `+a = 1` erronously being accepted. +- Fix not freeing a zero length String ### Stdlib changes - Increase BitWriter.write_bits limit up to 32 bits. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 71c5f718e..b89d97960 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -3083,7 +3083,6 @@ static void gencontext_emit_slice(GenContext *c, BEValue *be_value, Expr *expr) llvm_value_rvalue(c, &start); llvm_value_rvalue(c, &end); - // Calculate the size LLVMValueRef size; if (is_exclusive) @@ -3268,11 +3267,7 @@ static void llvm_emit_slice_assign(GenContext *c, BEValue *be_value, Expr *expr) llvm_emit_br(c, cond_block); // Finally set up our phi - if (!assign_block_end) - { - offset = start.value; - } - else + if (assign_block_end) { llvm_set_phi(offset, start.value, start_block, next_offset, assign_block_end); }