Skip to content

Commit 528df00

Browse files
authored
Merge pull request #176 from kubkon/safe-file-index
Migrate to safe File.Index
2 parents 531d86c + dd91714 commit 528df00

File tree

15 files changed

+290
-147
lines changed

15 files changed

+290
-147
lines changed

src/Atom.zig

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn getName(self: Atom, macho_file: *MachO) [:0]const u8 {
4242
}
4343

4444
pub fn getFile(self: Atom, macho_file: *MachO) File {
45-
return macho_file.getFile(self.file).?;
45+
return macho_file.getFile(self.file);
4646
}
4747

4848
pub fn toRef(self: Atom) Ref {
@@ -847,7 +847,7 @@ fn format2(
847847
const atom = ctx.atom;
848848
const macho_file = ctx.macho_file;
849849
const file = atom.getFile(macho_file);
850-
try writer.print("atom({d}) : {s} : @{x} : sect({d}) : align({x}) : size({x}) : thunk({d})", .{
850+
try writer.print("atom({}) : {s} : @{x} : sect({d}) : align({x}) : size({x}) : thunk({d})", .{
851851
atom.atom_index, atom.getName(macho_file), atom.getAddress(macho_file),
852852
atom.out_n_sect, atom.alignment, atom.size,
853853
atom.getExtra(macho_file).thunk,
@@ -876,10 +876,21 @@ pub const Index = enum(u32) {
876876
}
877877

878878
pub fn toRef(index: Index, file: File.Index) Ref {
879-
const result: Ref = @enumFromInt(@intFromEnum(index) | @as(u64, @intCast(file)) << 32);
879+
const result: Ref = @enumFromInt(@intFromEnum(index) | @as(u64, @intFromEnum(file)) << 32);
880880
assert(result != .none);
881881
return result;
882882
}
883+
884+
pub fn format(
885+
index: Index,
886+
comptime unused_fmt_string: []const u8,
887+
options: std.fmt.FormatOptions,
888+
writer: anytype,
889+
) !void {
890+
_ = unused_fmt_string;
891+
_ = options;
892+
try writer.print("{d}", .{@intFromEnum(index)});
893+
}
883894
};
884895

885896
pub const OptionalIndex = enum(u32) {
@@ -890,6 +901,21 @@ pub const OptionalIndex = enum(u32) {
890901
if (opt == .none) return null;
891902
return @enumFromInt(@intFromEnum(opt));
892903
}
904+
905+
pub fn format(
906+
opt: OptionalIndex,
907+
comptime unused_fmt_string: []const u8,
908+
options: std.fmt.FormatOptions,
909+
writer: anytype,
910+
) !void {
911+
_ = unused_fmt_string;
912+
_ = options;
913+
if (opt == .none) {
914+
try writer.writeAll(".none");
915+
} else {
916+
try writer.print("{d}", .{@intFromEnum(opt)});
917+
}
918+
}
893919
};
894920

895921
pub const Ref = enum(u64) {
@@ -900,7 +926,7 @@ pub const Ref = enum(u64) {
900926
if (ref == .none) return null;
901927
const raw = @intFromEnum(ref);
902928
const atom_index: Index = @enumFromInt(@as(u32, @truncate(raw)));
903-
const file_index: File.Index = @truncate(raw >> 32);
929+
const file_index: File.Index = @enumFromInt(@as(u32, @truncate(raw >> 32)));
904930
return .{ .atom = atom_index, .file = file_index };
905931
}
906932

@@ -918,7 +944,7 @@ pub const UnwrappedRef = struct {
918944
file: File.Index,
919945

920946
pub fn getAtom(ref: UnwrappedRef, macho_file: *MachO) *Atom {
921-
return macho_file.getFile(ref.file).?.getAtom(ref.atom);
947+
return macho_file.getFile(ref.file).getAtom(ref.atom);
922948
}
923949
};
924950

src/Dylib.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ pub fn writeSymtab(self: Dylib, macho_file: *MachO) void {
711711
}
712712

713713
pub inline fn getUmbrella(self: Dylib, macho_file: *MachO) *Dylib {
714-
return macho_file.getFile(self.umbrella).?.dylib;
714+
return macho_file.getFile(self.umbrella).dylib;
715715
}
716716

717717
fn addString(self: *Dylib, allocator: Allocator, name: []const u8) !MachO.String {
@@ -740,7 +740,7 @@ fn addSymbol(self: *Dylib, allocator: Allocator) !Symbol.Index {
740740
fn addSymbolAssumeCapacity(self: *Dylib) Symbol.Index {
741741
const index: Symbol.Index = @enumFromInt(self.symbols.items.len);
742742
const symbol = self.symbols.addOneAssumeCapacity();
743-
symbol.* = .{ .file = self.index };
743+
symbol.* = .{ .file = self.index.toOptional() };
744744
return index;
745745
}
746746

src/InternalObject.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub fn resolveBoundarySymbols(self: *InternalObject, macho_file: *MachO) !void {
166166
defer boundary_symbols.deinit();
167167

168168
for (macho_file.objects.items) |index| {
169-
const object = macho_file.getFile(index).?.object;
169+
const object = macho_file.getFile(index).object;
170170
for (object.symbols.items, 0..) |sym, i| {
171171
const sym_index: Symbol.Index = @enumFromInt(i);
172172
const nlist = object.symtab.items(.nlist)[i];
@@ -344,7 +344,7 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi
344344
defer objc_msgsend_syms.deinit();
345345

346346
for (macho_file.objects.items) |index| {
347-
const object = macho_file.getFile(index).?.object;
347+
const object = macho_file.getFile(index).object;
348348

349349
for (object.symbols.items, 0..) |sym, i| {
350350
const sym_index: Symbol.Index = @enumFromInt(i);
@@ -768,7 +768,7 @@ pub fn addSymbol(self: *InternalObject, allocator: Allocator) !Symbol.Index {
768768
pub fn addSymbolAssumeCapacity(self: *InternalObject) Symbol.Index {
769769
const index: Symbol.Index = @enumFromInt(self.symbols.items.len);
770770
const symbol = self.symbols.addOneAssumeCapacity();
771-
symbol.* = .{ .file = self.index };
771+
symbol.* = .{ .file = self.index.toOptional() };
772772
return index;
773773
}
774774

0 commit comments

Comments
 (0)