link: avoid double close on openPath error

This commit is contained in:
Andrew Kelley
2022-02-16 01:54:39 -08:00
parent 1a84c23d69
commit 2b3df5c81d
5 changed files with 15 additions and 21 deletions

View File

@@ -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.

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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));