From d7d81665a03af8c96fc1a004ff3b06ae0f475a2f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 13 Sep 2023 00:29:21 -0400 Subject: [PATCH] Fix miscellaneous nullability warnings --- BinaryObjectScanner.Wrappers/CFB.cs | 26 +++++++++++++++++++ .../PortableExecutable.cs | 12 +++++++-- BinaryObjectScanner.Wrappers/SGA.cs | 4 +-- BinaryObjectScanner.Wrappers/VPK.cs | 4 +++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/BinaryObjectScanner.Wrappers/CFB.cs b/BinaryObjectScanner.Wrappers/CFB.cs index 38a5a0ae..a8351328 100644 --- a/BinaryObjectScanner.Wrappers/CFB.cs +++ b/BinaryObjectScanner.Wrappers/CFB.cs @@ -308,7 +308,11 @@ namespace BinaryObjectScanner.Wrappers #endif { // If we have an invalid sector +#if NET48 if (startingSector < 0 || FATSectorNumbers == null || (long)startingSector >= FATSectorNumbers.Length) +#else + if (startingSector == null || startingSector < 0 || FATSectorNumbers == null || (long)startingSector >= FATSectorNumbers.Length) +#endif return null; // Setup the returned list @@ -321,8 +325,17 @@ namespace BinaryObjectScanner.Wrappers var lastSector = startingSector; while (true) { +#if NET6_0_OR_GREATER + if (lastSector == null) + break; +#endif + // Get the next sector from the lookup table +#if NET48 var nextSector = FATSectorNumbers[(uint)lastSector]; +#else + var nextSector = FATSectorNumbers[(uint)lastSector!.Value]; +#endif // If we have an end of chain or free sector if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT) @@ -412,7 +425,11 @@ namespace BinaryObjectScanner.Wrappers #endif { // If we have an invalid sector +#if NET48 if (startingSector < 0 || MiniFATSectorNumbers == null || (long)startingSector >= MiniFATSectorNumbers.Length) +#else + if (startingSector == null || startingSector < 0 || MiniFATSectorNumbers == null || (long)startingSector >= MiniFATSectorNumbers.Length) +#endif return null; // Setup the returned list @@ -425,8 +442,17 @@ namespace BinaryObjectScanner.Wrappers var lastSector = startingSector; while (true) { +#if NET6_0_OR_GREATER + if (lastSector == null) + break; +#endif + // Get the next sector from the lookup table +#if NET48 var nextSector = MiniFATSectorNumbers[(uint)lastSector]; +#else + var nextSector = MiniFATSectorNumbers[(uint)lastSector!.Value]; +#endif // If we have an end of chain or free sector if (nextSector == SabreTools.Models.CFB.SectorNumber.ENDOFCHAIN || nextSector == SabreTools.Models.CFB.SectorNumber.FREESECT) diff --git a/BinaryObjectScanner.Wrappers/PortableExecutable.cs b/BinaryObjectScanner.Wrappers/PortableExecutable.cs index 74001f3b..645cb3c4 100644 --- a/BinaryObjectScanner.Wrappers/PortableExecutable.cs +++ b/BinaryObjectScanner.Wrappers/PortableExecutable.cs @@ -1087,6 +1087,11 @@ namespace BinaryObjectScanner.Wrappers if (_stubExecutableData != null) return _stubExecutableData; +#if NET6_0_OR_GREATER + if (Stub_NewExeHeaderAddr == null) + return null; +#endif + // Populate the raw stub executable data based on the source int endOfStubHeader = 0x40; int lengthOfStubExecutableData = (int)Stub_NewExeHeaderAddr - endOfStubHeader; @@ -1133,7 +1138,7 @@ namespace BinaryObjectScanner.Wrappers #if NET48 public Dictionary ResourceData #else - public Dictionary? ResourceData + public Dictionary? ResourceData #endif { get @@ -4403,7 +4408,10 @@ namespace BinaryObjectScanner.Wrappers return -1; // Otherwise, find the section it exists within - return OH_AddressOfEntryPoint.Value.ContainingSectionIndex(SectionTable); + return OH_AddressOfEntryPoint.Value.ContainingSectionIndex(SectionTable + .Where(sh => sh != null) + .Cast() + .ToArray()); #endif } diff --git a/BinaryObjectScanner.Wrappers/SGA.cs b/BinaryObjectScanner.Wrappers/SGA.cs index 8f9dac85..dd8b4f64 100644 --- a/BinaryObjectScanner.Wrappers/SGA.cs +++ b/BinaryObjectScanner.Wrappers/SGA.cs @@ -852,7 +852,7 @@ namespace BinaryObjectScanner.Wrappers // Reverse and assemble the filename parentNames.Reverse(); - filename = Path.Combine(parentNames.ToArray()); + filename = Path.Combine(parentNames.Cast().ToArray()); // Get the file offset long fileOffset; @@ -866,7 +866,7 @@ namespace BinaryObjectScanner.Wrappers } // Adjust the file offset - fileOffset += FileDataOffset.Value; + fileOffset += FileDataOffset ?? 0; // Get the file sizes long fileSize, outputFileSize; diff --git a/BinaryObjectScanner.Wrappers/VPK.cs b/BinaryObjectScanner.Wrappers/VPK.cs index f1e02344..eb815874 100644 --- a/BinaryObjectScanner.Wrappers/VPK.cs +++ b/BinaryObjectScanner.Wrappers/VPK.cs @@ -152,7 +152,11 @@ namespace BinaryObjectScanner.Wrappers /// /// Array of archive filenames attached to the given VPK /// +#if NET48 private string[] _archiveFilenames = null; +#else + private string[]? _archiveFilenames = null; +#endif #endregion