From 4bb3f625dddb7ef4d902c2c4727badf7927b8fc0 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 20 Jun 2024 11:23:28 -0400 Subject: [PATCH] Make PE debug table parsing safer --- .../Wrappers/PortableExecutable.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/SabreTools.Serialization/Wrappers/PortableExecutable.cs b/SabreTools.Serialization/Wrappers/PortableExecutable.cs index 92eb3578..7dcfa51a 100644 --- a/SabreTools.Serialization/Wrappers/PortableExecutable.cs +++ b/SabreTools.Serialization/Wrappers/PortableExecutable.cs @@ -962,9 +962,18 @@ namespace SabreTools.Serialization.Wrappers uint address = entry.PointerToRawData; uint size = entry.SizeOfData; - byte[]? entryData = ReadFromDataSource((int)address, (int)size); - if (entryData == null) - continue; + // Read the entry data until we have the end of the stream + byte[]? entryData; + try + { + entryData = ReadFromDataSource((int)address, (int)size); + if (entryData == null || entryData.Length < 4) + continue; + } + catch (EndOfStreamException) + { + return; + } // If we have CodeView debug data, try to parse it if (entry.DebugType == Models.PortableExecutable.DebugType.IMAGE_DEBUG_TYPE_CODEVIEW)