diff --git a/SabreTools.Serialization/Deserializers/PortableExecutable.cs b/SabreTools.Serialization/Deserializers/PortableExecutable.cs
index d5fe299e..5162219a 100644
--- a/SabreTools.Serialization/Deserializers/PortableExecutable.cs
+++ b/SabreTools.Serialization/Deserializers/PortableExecutable.cs
@@ -192,6 +192,7 @@ namespace SabreTools.Serialization.Deserializers
Array.Resize(ref localEntries, localEntries.Length + 1);
pex.ResourceDirectoryTable.Entries = localEntries;
int length = (int)(tableSize - (data.Position - tableStart));
+ length = (int)Math.Min(length, data.Length - data.Position);
pex.ResourceDirectoryTable.Entries[localEntries.Length - 1] = new ResourceDirectoryEntry
{
@@ -357,8 +358,9 @@ namespace SabreTools.Serialization.Deserializers
/// Parse a Stream into an BaseRelocationBlock
///
/// Stream to parse
+ /// First address not part of the base relocation table
/// Filled BaseRelocationBlock on success, null on error
- public static BaseRelocationBlock? ParseBaseRelocationBlock(Stream data)
+ public static BaseRelocationBlock? ParseBaseRelocationBlock(Stream data, long endOffset)
{
var obj = new BaseRelocationBlock();
@@ -370,6 +372,8 @@ namespace SabreTools.Serialization.Deserializers
// Guard against invalid block sizes
if (obj.BlockSize % 2 != 0)
return obj;
+ if (data.Position + obj.BlockSize > endOffset)
+ return obj;
int entryCount = ((int)obj.BlockSize - 8) / 2;
obj.TypeOffsetFieldEntries = new BaseRelocationTypeOffsetFieldEntry[entryCount];
@@ -396,7 +400,7 @@ namespace SabreTools.Serialization.Deserializers
while (data.Position < endOffset && data.Position < data.Length)
{
- var block = ParseBaseRelocationBlock(data);
+ var block = ParseBaseRelocationBlock(data, endOffset);
if (block == null)
break;
diff --git a/SabreTools.Serialization/Printers/PortableExecutable.cs b/SabreTools.Serialization/Printers/PortableExecutable.cs
index c3ddc365..9f2ffdfe 100644
--- a/SabreTools.Serialization/Printers/PortableExecutable.cs
+++ b/SabreTools.Serialization/Printers/PortableExecutable.cs
@@ -557,16 +557,8 @@ namespace SabreTools.Serialization.Printers
var typeOffsetFieldEntry = baseRelocationTableEntry.TypeOffsetFieldEntries[j];
builder.AppendLine($" Type and Offset Entry {j}");
- if (typeOffsetFieldEntry == null)
- {
- builder.AppendLine($" Type: [NULL]");
- builder.AppendLine($" Offset: [NULL]");
- }
- else
- {
- builder.AppendLine($" Type: {typeOffsetFieldEntry.BaseRelocationType} (0x{typeOffsetFieldEntry.BaseRelocationType:X})");
- builder.AppendLine(typeOffsetFieldEntry.Offset, " Offset");
- }
+ builder.AppendLine($" Type: {typeOffsetFieldEntry.BaseRelocationType} (0x{typeOffsetFieldEntry.BaseRelocationType:X})");
+ builder.AppendLine(typeOffsetFieldEntry.Offset, " Offset");
}
}