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...@@ -134,16 +134,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
134 return createEmpty(allocator, options);134 return createEmpty(allocator, options);
135 }135 }
136136
137 const self = try createEmpty(allocator, options);
138 errdefer self.base.destroy();
139
137 const file = try options.emit.?.directory.handle.createFile(sub_path, .{140 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
138 .truncate = false,141 .truncate = false,
139 .read = true,142 .read = true,
140 .mode = link.determineMode(options),143 .mode = link.determineMode(options),
141 });144 });
142 errdefer file.close();
143
144 const self = try createEmpty(allocator, options);
145 errdefer self.base.destroy();
146
147 self.base.file = file;145 self.base.file = file;
148146
149 // TODO Write object specific relocations, COFF symbol table, then enable object file output.147 // 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...@@ -299,15 +299,14 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
299 return createEmpty(allocator, options);299 return createEmpty(allocator, options);
300 }300 }
301301
302 const self = try createEmpty(allocator, options);
303 errdefer self.base.destroy();
304
302 const file = try options.emit.?.directory.handle.createFile(sub_path, .{305 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
303 .truncate = false,306 .truncate = false,
304 .read = true,307 .read = true,
305 .mode = link.determineMode(options),308 .mode = link.determineMode(options),
306 });309 });
307 errdefer file.close();
308
309 const self = try createEmpty(allocator, options);
310 errdefer self.base.destroy();
311310
312 self.base.file = file;311 self.base.file = file;
313 self.shdr_table_dirty = true;312 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...@@ -643,14 +643,16 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
643 if (options.use_llvm)643 if (options.use_llvm)
644 return error.LLVMBackendDoesNotSupportPlan9;644 return error.LLVMBackendDoesNotSupportPlan9;
645 assert(options.object_format == .plan9);645 assert(options.object_format == .plan9);
646
647 const self = try createEmpty(allocator, options);
648 errdefer self.base.destroy();
649
646 const file = try options.emit.?.directory.handle.createFile(sub_path, .{650 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
647 .read = true,651 .read = true,
648 .mode = link.determineMode(options),652 .mode = link.determineMode(options),
649 });653 });
650 errdefer file.close();654 errdefer file.close();
651655 self.base.file = file;
652 const self = try createEmpty(allocator, options);
653 errdefer self.base.destroy();
654656
655 self.bases = defaultBaseAddrs(options.target.cpu.arch);657 self.bases = defaultBaseAddrs(options.target.cpu.arch);
656658
...@@ -673,7 +675,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -673,7 +675,6 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
673 },675 },
674 });676 });
675677
676 self.base.file = file;
677 return self;678 return self;
678}679}
679680
src/link/SpirV.zig+2-4
...@@ -104,13 +104,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -104,13 +104,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
104 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForSpirV; // TODO: LLVM Doesn't support SpirV at all.104 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForSpirV; // TODO: LLVM Doesn't support SpirV at all.
105 if (options.use_lld) return error.LLD_LinkingIsTODO_ForSpirV; // TODO: LLD Doesn't support SpirV at all.105 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
111 const spirv = try createEmpty(allocator, options);107 const spirv = try createEmpty(allocator, options);
112 errdefer spirv.base.destroy();108 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 });
114 spirv.base.file = file;112 spirv.base.file = file;
115 return spirv;113 return spirv;
116}114}
src/link/Wasm.zig+2-4
...@@ -105,13 +105,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -105,13 +105,11 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
105 return createEmpty(allocator, options);105 return createEmpty(allocator, options);
106 }106 }
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
112 const wasm_bin = try createEmpty(allocator, options);108 const wasm_bin = try createEmpty(allocator, options);
113 errdefer wasm_bin.base.destroy();109 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 });
115 wasm_bin.base.file = file;113 wasm_bin.base.file = file;
116114
117 try file.writeAll(&(wasm.magic ++ wasm.version));115 try file.writeAll(&(wasm.magic ++ wasm.version));