zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 35c681b7b18df1f8679457975f9dcfc6a4a53468 (tree)
parent 8902f3ca32c22656e1f11b562dc3ad6030da14ac
Author: Layne Gustafson <lgustaf1@binghamton.edu>
Date:   Thu, 16 Jan 2020 15:26:53 -0500

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.

Diffstat:
Msrc-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 @@ -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(), }; } };