From eaa5bb566227dd884b4f93e091630dec9a12674f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 1 Oct 2025 20:06:05 -0400 Subject: [PATCH] Sections can't be null --- .../Readers/PortableExecutable.cs | 4 +++ .../Wrappers/PortableExecutable.cs | 30 +++++-------------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/SabreTools.Serialization/Readers/PortableExecutable.cs b/SabreTools.Serialization/Readers/PortableExecutable.cs index 2afb3ce3..b7f1ba4e 100644 --- a/SabreTools.Serialization/Readers/PortableExecutable.cs +++ b/SabreTools.Serialization/Readers/PortableExecutable.cs @@ -106,6 +106,10 @@ namespace SabreTools.Serialization.Readers #endregion + // Cache the overlay offset + long endOfSectionData = optionalHeader?.SizeOfHeaders ?? 0; + Array.ForEach(pex.SectionTable, s => endOfSectionData += s.SizeOfRawData); + #region Symbol Table and String Table offset = initialOffset + fileHeader.PointerToSymbolTable; diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index e3dd8a8e..936b0218 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -90,7 +90,7 @@ namespace SabreTools.Serialization.Wrappers // If the entry point matches with the start of a section, use that int entryPointSection = FindEntryPointSectionIndex(); - if (entryPointSection >= 0 && OptionalHeader.AddressOfEntryPoint == SectionTable[entryPointSection]?.VirtualAddress) + if (entryPointSection >= 0 && OptionalHeader.AddressOfEntryPoint == SectionTable[entryPointSection].VirtualAddress) { _entryPointData = GetSectionData(entryPointSection) ?? []; return _entryPointData; @@ -143,15 +143,15 @@ namespace SabreTools.Serialization.Wrappers // Populate the raw header padding data based on the source uint headerStartAddress = Stub.Header.NewExeHeaderAddr; uint firstSectionAddress = uint.MaxValue; - foreach (var s in SectionTable) + foreach (var section in SectionTable) { - if (s == null || s.PointerToRawData == 0) + if (section.PointerToRawData == 0) continue; - if (s.PointerToRawData < headerStartAddress) + if (section.PointerToRawData < headerStartAddress) continue; - if (s.PointerToRawData < firstSectionAddress) - firstSectionAddress = s.PointerToRawData; + if (section.PointerToRawData < firstSectionAddress) + firstSectionAddress = section.PointerToRawData; } // Check if the header length is more than 0 before reading data @@ -328,15 +328,7 @@ namespace SabreTools.Serialization.Wrappers // Search through all sections and find the furthest a section goes long endOfSectionData = OptionalHeader.SizeOfHeaders; - foreach (var section in SectionTable) - { - // If we have an invalid section - if (section == null) - continue; - - // Add the raw data size - endOfSectionData += section.SizeOfRawData; - } + Array.ForEach(SectionTable, s => endOfSectionData += s.SizeOfRawData); // If we didn't find the end of section data if (endOfSectionData <= 0) @@ -527,11 +519,8 @@ namespace SabreTools.Serialization.Wrappers _sectionNames = new string[SectionTable.Length]; for (int i = 0; i < _sectionNames.Length; i++) { - var section = SectionTable[i]; - if (section == null) - continue; - // TODO: Handle long section names with leading `/` + var section = SectionTable[i]; byte[]? sectionNameBytes = section.Name; if (sectionNameBytes != null) { @@ -2046,9 +2035,6 @@ namespace SabreTools.Serialization.Wrappers // Get the section data from the table var section = SectionTable[index]; - if (section == null) - return null; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); if (address == 0) return null;