Commit 8a5f331ec8

kcbanner <kcbanner@gmail.com>
2023-08-15 08:44:34
coff: handle the case of there being no PDB path
1 parent 5b86180
Changed files (2)
lib/std/coff.zig
@@ -1101,12 +1101,13 @@ pub const Coff = struct {
         return coff;
     }
 
-    pub fn getPdbPath(self: *Coff, buffer: []u8) !usize {
+    pub fn getPdbPath(self: *Coff, buffer: []u8) !?usize {
         assert(self.is_image);
 
         const data_dirs = self.getDataDirectories();
-        const debug_dir = data_dirs[@intFromEnum(DirectoryEntry.DEBUG)];
+        if (@intFromEnum(DirectoryEntry.DEBUG) >= data_dirs.len) return null;
 
+        const debug_dir = data_dirs[@intFromEnum(DirectoryEntry.DEBUG)];
         var stream = std.io.fixedBufferStream(self.data);
         const reader = stream.reader();
 
@@ -1126,14 +1127,14 @@ pub const Coff = struct {
         // It can be in any section.
         const debug_dir_entry_count = debug_dir.size / @sizeOf(DebugDirectoryEntry);
         var i: u32 = 0;
-        blk: while (i < debug_dir_entry_count) : (i += 1) {
+        while (i < debug_dir_entry_count) : (i += 1) {
             const debug_dir_entry = try reader.readStruct(DebugDirectoryEntry);
             if (debug_dir_entry.type == .CODEVIEW) {
                 const dir_offset = if (self.is_loaded) debug_dir_entry.address_of_raw_data else debug_dir_entry.pointer_to_raw_data;
                 try stream.seekTo(dir_offset);
-                break :blk;
+                break;
             }
-        }
+        } else return null;
 
         var cv_signature: [4]u8 = undefined; // CodeView signature
         try reader.readNoEof(cv_signature[0..]);
lib/std/debug.zig
@@ -1024,12 +1024,8 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_obj: *coff.Coff) !ModuleDebu
             di.dwarf = dwarf;
         }
 
-        // Only used by the pdb path
-        di.coff_section_headers = try coff_obj.getSectionHeadersAlloc(allocator);
-        errdefer allocator.free(di.coff_section_headers);
-
         var path_buf: [windows.MAX_PATH]u8 = undefined;
-        const len = try coff_obj.getPdbPath(path_buf[0..]);
+        const len = try coff_obj.getPdbPath(path_buf[0..]) orelse return di;
         const raw_path = path_buf[0..len];
 
         const path = try fs.path.resolve(allocator, &[_][]const u8{raw_path});
@@ -1038,8 +1034,6 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_obj: *coff.Coff) !ModuleDebu
         di.pdb = pdb.Pdb.init(allocator, path) catch |err| switch (err) {
             error.FileNotFound, error.IsDir => {
                 if (di.dwarf == null) return error.MissingDebugInfo;
-                allocator.free(di.coff_section_headers);
-                di.coff_section_headers = undefined;
                 return di;
             },
             else => return err,
@@ -1050,6 +1044,10 @@ fn readCoffDebugInfo(allocator: mem.Allocator, coff_obj: *coff.Coff) !ModuleDebu
         if (!mem.eql(u8, &coff_obj.guid, &di.pdb.?.guid) or coff_obj.age != di.pdb.?.age)
             return error.InvalidDebugInfo;
 
+        // Only used by the pdb path
+        di.coff_section_headers = try coff_obj.getSectionHeadersAlloc(allocator);
+        errdefer allocator.free(di.coff_section_headers);
+
         return di;
     }
 }