authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-06-19 17:54:25-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-06-19 18:41:11-04:00
loge92b12906338207975ee73aa99db63cb9240bf04
tree6fa7801d2b900d02067f48d700f8d9194be384f9
parent917640810e7f3e18daff9e75b5ecefe761a1896c

Compilation: fix use after free

Closes #23967

1 files changed, 16 insertions(+), 6 deletions(-)

src/Compilation.zig+16-6
...@@ -1990,9 +1990,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -1990,9 +1990,6 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
1990 };1990 };
1991 errdefer if (opt_zcu) |zcu| zcu.deinit();1991 errdefer if (opt_zcu) |zcu| zcu.deinit();
19921992
1993 var windows_libs = try std.StringArrayHashMapUnmanaged(void).init(gpa, options.windows_lib_names, &.{});
1994 errdefer windows_libs.deinit(gpa);
1995
1996 comp.* = .{1993 comp.* = .{
1997 .gpa = gpa,1994 .gpa = gpa,
1998 .arena = arena,1995 .arena = arena,
...@@ -2037,7 +2034,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -2037,7 +2034,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
2037 .incremental = options.incremental,2034 .incremental = options.incremental,
2038 .root_name = root_name,2035 .root_name = root_name,
2039 .sysroot = sysroot,2036 .sysroot = sysroot,
2040 .windows_libs = windows_libs,2037 .windows_libs = .empty,
2041 .version = options.version,2038 .version = options.version,
2042 .libc_installation = libc_dirs.libc_installation,2039 .libc_installation = libc_dirs.libc_installation,
2043 .compiler_rt_strat = compiler_rt_strat,2040 .compiler_rt_strat = compiler_rt_strat,
...@@ -2065,6 +2062,13 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -2065,6 +2062,13 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
2065 .emit_docs = try options.emit_docs.resolve(arena, &options, .docs),2062 .emit_docs = try options.emit_docs.resolve(arena, &options, .docs),
2066 };2063 };
20672064
2065 errdefer {
2066 for (comp.windows_libs.keys()) |windows_lib| gpa.free(windows_lib);
2067 comp.windows_libs.deinit(gpa);
2068 }
2069 try comp.windows_libs.ensureUnusedCapacity(gpa, options.windows_lib_names.len);
2070 for (options.windows_lib_names) |windows_lib| comp.windows_libs.putAssumeCapacity(try gpa.dupe(u8, windows_lib), {});
2071
2068 // Prevent some footguns by making the "any" fields of config reflect2072 // Prevent some footguns by making the "any" fields of config reflect
2069 // the default Module settings.2073 // the default Module settings.
2070 comp.config.any_unwind_tables = any_unwind_tables;2074 comp.config.any_unwind_tables = any_unwind_tables;
...@@ -2387,7 +2391,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil...@@ -2387,7 +2391,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
23872391
2388 // When linking mingw-w64 there are some import libs we always need.2392 // When linking mingw-w64 there are some import libs we always need.
2389 try comp.windows_libs.ensureUnusedCapacity(gpa, mingw.always_link_libs.len);2393 try comp.windows_libs.ensureUnusedCapacity(gpa, mingw.always_link_libs.len);
2390 for (mingw.always_link_libs) |name| comp.windows_libs.putAssumeCapacity(name, {});2394 for (mingw.always_link_libs) |name| comp.windows_libs.putAssumeCapacity(try gpa.dupe(u8, name), {});
2391 } else {2395 } else {
2392 return error.LibCUnavailable;2396 return error.LibCUnavailable;
2393 }2397 }
...@@ -2480,6 +2484,7 @@ pub fn destroy(comp: *Compilation) void {...@@ -2480,6 +2484,7 @@ pub fn destroy(comp: *Compilation) void {
2480 comp.c_object_work_queue.deinit();2484 comp.c_object_work_queue.deinit();
2481 comp.win32_resource_work_queue.deinit();2485 comp.win32_resource_work_queue.deinit();
24822486
2487 for (comp.windows_libs.keys()) |windows_lib| gpa.free(windows_lib);
2483 comp.windows_libs.deinit(gpa);2488 comp.windows_libs.deinit(gpa);
24842489
2485 {2490 {
...@@ -7563,7 +7568,12 @@ pub fn addLinkLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -7563,7 +7568,12 @@ pub fn addLinkLib(comp: *Compilation, lib_name: []const u8) !void {
7563 // If we haven't seen this library yet and we're targeting Windows, we need7568 // If we haven't seen this library yet and we're targeting Windows, we need
7564 // to queue up a work item to produce the DLL import library for this.7569 // to queue up a work item to produce the DLL import library for this.
7565 const gop = try comp.windows_libs.getOrPut(comp.gpa, lib_name);7570 const gop = try comp.windows_libs.getOrPut(comp.gpa, lib_name);
7566 if (!gop.found_existing) try comp.queueJob(.{ .windows_import_lib = comp.windows_libs.count() - 1 });7571 if (gop.found_existing) return;
7572 {
7573 errdefer _ = comp.windows_libs.pop();
7574 gop.key_ptr.* = try comp.gpa.dupe(u8, lib_name);
7575 }
7576 try comp.queueJob(.{ .windows_import_lib = gop.index });
7567}7577}
75687578
7569/// This decides the optimization mode for all zig-provided libraries, including7579/// This decides the optimization mode for all zig-provided libraries, including