macho: handle empty string in ZigObject.getString

This commit is contained in:
Jakub Konka 2024-07-22 13:35:55 +02:00
parent 96fa29b90f
commit 8541119e9b
3 changed files with 5 additions and 2 deletions

View File

@ -42,7 +42,6 @@ extra: u32 = 0,
pub fn getName(self: Atom, macho_file: *MachO) [:0]const u8 {
return switch (self.getFile(macho_file)) {
.dylib => unreachable,
.zig_object => |x| x.strtab.buffer.items[self.name.pos..][0 .. self.name.len - 1 :0],
inline else => |x| x.getString(self.name),
};
}

View File

@ -57,7 +57,6 @@ pub fn weakRef(symbol: Symbol, macho_file: *MachO) bool {
pub fn getName(symbol: Symbol, macho_file: *MachO) [:0]const u8 {
return switch (symbol.getFile(macho_file).?) {
.zig_object => |x| x.strtab.buffer.items[symbol.name.pos..][0 .. symbol.name.len - 1 :0],
inline else => |x| x.getString(symbol.name),
};
}

View File

@ -1767,6 +1767,11 @@ fn addString(self: *ZigObject, allocator: Allocator, string: []const u8) !MachO.
return .{ .pos = off, .len = @intCast(string.len + 1) };
}
pub fn getString(self: ZigObject, string: MachO.String) [:0]const u8 {
if (string.len == 0) return "";
return self.strtab.buffer.items[string.pos..][0 .. string.len - 1 :0];
}
pub fn asFile(self: *ZigObject) File {
return .{ .zig_object = self };
}