From df9462690f9ca28e1b0fc3185f319c335cc3a32c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 6 Oct 2023 12:42:58 +0200 Subject: [PATCH] std: fix memory bug in getExternalExecutor Until now, we would pass `candidate: NativeTargetInfo` which creates a copy of the `NativeTargetInfo.DynamicLinker` buffer. We would then return this buffer in `bad_dl: []const u8` which would goes out-of-scope the moment we leave this function frame yielding garbage. To fix this, we just need to remember to pass by const-pointer `candidate: *const NativeTargetInfo`. --- lib/std/Build/Step/Run.zig | 2 +- lib/std/zig/system/NativeTargetInfo.zig | 2 +- src/main.zig | 6 +++--- test/src/Cases.zig | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index 66e9224fe5..9d8f26559c 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -679,7 +679,7 @@ fn runCommand( } const need_cross_glibc = exe.target.isGnuLibC() and exe.is_linking_libc; - switch (b.host.getExternalExecutor(exe.target_info, .{ + switch (b.host.getExternalExecutor(&exe.target_info, .{ .qemu_fixes_dl = need_cross_glibc and b.glibc_runtimes_dir != null, .link_libc = exe.is_linking_libc, })) { diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig index 6691e0f3cb..bcb60d968e 100644 --- a/lib/std/zig/system/NativeTargetInfo.zig +++ b/lib/std/zig/system/NativeTargetInfo.zig @@ -1002,7 +1002,7 @@ pub const GetExternalExecutorOptions = struct { /// of the other target. pub fn getExternalExecutor( host: NativeTargetInfo, - candidate: NativeTargetInfo, + candidate: *const NativeTargetInfo, options: GetExternalExecutorOptions, ) Executor { const os_match = host.target.os.tag == candidate.target.os.tag; diff --git a/src/main.zig b/src/main.zig index f7f3da1b99..247669440d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3667,7 +3667,7 @@ fn buildOutputType( test_exec_args.items, self_exe_path.?, arg_mode, - target_info, + &target_info, &comp_destroyed, all_args, runtime_args_start, @@ -3995,7 +3995,7 @@ fn runOrTest( test_exec_args: []const ?[]const u8, self_exe_path: []const u8, arg_mode: ArgMode, - target_info: std.zig.system.NativeTargetInfo, + target_info: *const std.zig.system.NativeTargetInfo, comp_destroyed: *bool, all_args: []const []const u8, runtime_args_start: ?usize, @@ -6256,7 +6256,7 @@ fn parseIntSuffix(arg: []const u8, prefix_len: usize) u64 { fn warnAboutForeignBinaries( arena: Allocator, arg_mode: ArgMode, - target_info: std.zig.system.NativeTargetInfo, + target_info: *const std.zig.system.NativeTargetInfo, link_libc: bool, ) !void { const host_cross_target: std.zig.CrossTarget = .{}; diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 1252da0e95..fe1fb6fe34 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -590,7 +590,7 @@ pub fn lowerToBuildSteps( const run = if (case.target.ofmt == .c) run_step: { const target_info = std.zig.system.NativeTargetInfo.detect(case.target) catch |err| std.debug.panic("unable to detect notive host: {s}\n", .{@errorName(err)}); - if (host.getExternalExecutor(target_info, .{ .link_libc = true }) != .native) { + if (host.getExternalExecutor(&target_info, .{ .link_libc = true }) != .native) { // We wouldn't be able to run the compiled C code. break :no_exec; }