From 35c681b7b18df1f8679457975f9dcfc6a4a53468 Mon Sep 17 00:00:00 2001 From: Layne Gustafson Date: Thu, 16 Jan 2020 15:26:53 -0500 Subject: [PATCH] Fix sentinel mismatch in llvm strings Previously, buffers were used with toOwnedSlice() to create c strings for LLVM cpu/feature strings. However, toOwnedSlice() shrinks the string memory to the buffer's length, which cuts off the null terminator. Now toSliceConst() is used instead, and the buffer is not deinited so that the string memory is not freed. --- src-self-hosted/stage1.zig | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 98bd677c27..3e52a670a3 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -643,20 +643,20 @@ const Stage2TargetDetails = struct { var builtin_str_buffer = try std.Buffer.init( allocator, "@import(\"std\").target.TargetDetails{.cpu=&@import(\"std\").target."); - defer builtin_str_buffer.deinit(); try builtin_str_buffer.append(@tagName(arch)); try builtin_str_buffer.append(".cpu_"); try builtin_str_buffer.append(cpu.name); try builtin_str_buffer.append("};"); + return Self{ .allocator = allocator, .target_details = .{ .cpu = cpu, }, - .llvm_cpu_str = try toNullTerminatedStringAlloc(allocator, cpu.name), + .llvm_cpu_str = try toNullTerminatedStringAlloc(allocator, cpu.llvm_name), .llvm_features_str = null_terminated_empty_string, - .builtin_str = builtin_str_buffer.toOwnedSlice(), + .builtin_str = builtin_str_buffer.toSliceConst(), }; } @@ -664,10 +664,8 @@ const Stage2TargetDetails = struct { var builtin_str_buffer = try std.Buffer.init( allocator, "@import(\"std\").target.TargetDetails{.features=&[_]*const @import(\"std\").target.Feature{\n"); - defer builtin_str_buffer.deinit(); var llvm_features_buffer = try std.Buffer.initSize(allocator, 0); - defer llvm_features_buffer.deinit(); // First, disable all features. // This way, we only get the ones the user requests. @@ -691,17 +689,14 @@ const Stage2TargetDetails = struct { try builtin_str_buffer.append("}};"); - // This is needed here because llvm_features_buffer.len() is no longer valid after toOwnedSlice(). - const llvm_features_buffer_len = llvm_features_buffer.len(); - return Self{ .allocator = allocator, .target_details = std.target.TargetDetails{ .features = features, }, .llvm_cpu_str = null_terminated_empty_string, - .llvm_features_str = llvm_features_buffer.toOwnedSlice()[0..llvm_features_buffer_len :0], - .builtin_str = builtin_str_buffer.toOwnedSlice(), + .llvm_features_str = llvm_features_buffer.toSliceConst(), + .builtin_str = builtin_str_buffer.toSliceConst(), }; } };