authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-16 01:54:39-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-16 14:12:15-07:00
log2b3df5c81d8455856c2b355694ce818385218f36
treef743e1eda1d8b497836fbeac75545d3d119c9844
parent1a84c23d69c7e26ba1916c1f9f0e6002ff40d116

link: avoid double close on openPath error


5 files changed, 15 insertions(+), 21 deletions(-)

src/link/Coff.zig+3-5
......@@ -134,16 +134,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
134134 return createEmpty(allocator, options);
135135 }
136136
137 const self = try createEmpty(allocator, options);
138 errdefer self.base.destroy();
139
137140 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
138141 .truncate = false,
139142 .read = true,
140143 .mode = link.determineMode(options),
141144 });
142 errdefer file.close();
143
144 const self = try createEmpty(allocator, options);
145 errdefer self.base.destroy();
146
147145 self.base.file = file;
148146
149147 // TODO Write object specific relocations, COFF symbol table, then enable object file output.
src/link/Elf.zig+3-4
......@@ -299,15 +299,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
299299 return createEmpty(allocator, options);
300300 }
301301
302 const self = try createEmpty(allocator, options);
303 errdefer self.base.destroy();
304
302305 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
303306 .truncate = false,
304307 .read = true,
305308 .mode = link.determineMode(options),
306309 });
307 errdefer file.close();
308
309 const self = try createEmpty(allocator, options);
310 errdefer self.base.destroy();
311310
312311 self.base.file = file;
313312 self.shdr_table_dirty = true;
src/link/Plan9.zig+5-4
......@@ -643,14 +643,16 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
643643 if (options.use_llvm)
644644 return error.LLVMBackendDoesNotSupportPlan9;
645645 assert(options.object_format == .plan9);
646
647 const self = try createEmpty(allocator, options);
648 errdefer self.base.destroy();
649
646650 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
647651 .read = true,
648652 .mode = link.determineMode(options),
649653 });
650654 errdefer file.close();
651
652 const self = try createEmpty(allocator, options);
653 errdefer self.base.destroy();
655 self.base.file = file;
654656
655657 self.bases = defaultBaseAddrs(options.target.cpu.arch);
656658
......@@ -673,7 +675,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
673675 },
674676 });
675677
676 self.base.file = file;
677678 return self;
678679}
679680
src/link/SpirV.zig+2-4
......@@ -104,13 +104,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
104104 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForSpirV; // TODO: LLVM Doesn't support SpirV at all.
105105 if (options.use_lld) return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all.
106106
107 // TODO: read the file and keep valid parts instead of truncating
108 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
109 errdefer file.close();
110
111107 const spirv = try createEmpty(allocator, options);
112108 errdefer spirv.base.destroy();
113109
110 // TODO: read the file and keep valid parts instead of truncating
111 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
114112 spirv.base.file = file;
115113 return spirv;
116114}
src/link/Wasm.zig+2-4
......@@ -105,13 +105,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
105105 return createEmpty(allocator, options);
106106 }
107107
108 // TODO: read the file and keep valid parts instead of truncating
109 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
110 errdefer file.close();
111
112108 const wasm_bin = try createEmpty(allocator, options);
113109 errdefer wasm_bin.base.destroy();
114110
111 // TODO: read the file and keep valid parts instead of truncating
112 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
115113 wasm_bin.base.file = file;
116114
117115 try file.writeAll(&(wasm.magic ++ wasm.version));