From 2b3df5c81d8455856c2b355694ce818385218f36 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 16 Feb 2022 01:54:39 -0800 Subject: [PATCH] link: avoid double close on openPath error --- src/link/Coff.zig | 8 +++----- src/link/Elf.zig | 7 +++---- src/link/Plan9.zig | 9 +++++---- src/link/SpirV.zig | 6 ++---- src/link/Wasm.zig | 6 ++---- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 32d4d38235..3ee34682e9 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -134,16 +134,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option return createEmpty(allocator, options); } + const self = try createEmpty(allocator, options); + errdefer self.base.destroy(); + const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options), }); - errdefer file.close(); - - const self = try createEmpty(allocator, options); - errdefer self.base.destroy(); - self.base.file = file; // TODO Write object specific relocations, COFF symbol table, then enable object file output. diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 467bbeee54..08903f7c05 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -299,15 +299,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option return createEmpty(allocator, options); } + const self = try createEmpty(allocator, options); + errdefer self.base.destroy(); + const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options), }); - errdefer file.close(); - - const self = try createEmpty(allocator, options); - errdefer self.base.destroy(); self.base.file = file; self.shdr_table_dirty = true; diff --git a/src/link/Plan9.zig b/src/link/Plan9.zig index 4eaa6ed26b..4648a6a673 100644 --- a/src/link/Plan9.zig +++ b/src/link/Plan9.zig @@ -643,14 +643,16 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option if (options.use_llvm) return error.LLVMBackendDoesNotSupportPlan9; assert(options.object_format == .plan9); + + const self = try createEmpty(allocator, options); + errdefer self.base.destroy(); + const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .read = true, .mode = link.determineMode(options), }); errdefer file.close(); - - const self = try createEmpty(allocator, options); - errdefer self.base.destroy(); + self.base.file = file; self.bases = defaultBaseAddrs(options.target.cpu.arch); @@ -673,7 +675,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option }, }); - self.base.file = file; return self; } diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index ee3205c9f5..19cb1373ef 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -104,13 +104,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option if (options.use_llvm) return error.LLVM_BackendIsTODO_ForSpirV; // TODO: LLVM Doesn't support SpirV at all. if (options.use_lld) return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all. - // TODO: read the file and keep valid parts instead of truncating - const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true }); - errdefer file.close(); - const spirv = try createEmpty(allocator, options); errdefer spirv.base.destroy(); + // TODO: read the file and keep valid parts instead of truncating + const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true }); spirv.base.file = file; return spirv; } diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 81d77d5b66..8f6dfacf46 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -105,13 +105,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option return createEmpty(allocator, options); } - // TODO: read the file and keep valid parts instead of truncating - const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true }); - errdefer file.close(); - const wasm_bin = try createEmpty(allocator, options); errdefer wasm_bin.base.destroy(); + // TODO: read the file and keep valid parts instead of truncating + const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true }); wasm_bin.base.file = file; try file.writeAll(&(wasm.magic ++ wasm.version));