Skip to content

Commit 6d2e2d8

Browse files
committed
Handle some invalid parsing cases that were missed previously
1 parent a2b0815 commit 6d2e2d8

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

SabreTools.Serialization/Deserializers/NewExecutable.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,14 @@ public static PerSegmentData ParsePerSegmentData(Stream data)
420420
obj.RelocationRecords = new RelocationRecord[obj.RelocationRecordCount];
421421
for (int i = 0; i < obj.RelocationRecords.Length; i++)
422422
{
423-
obj.RelocationRecords[i] = ParseRelocationRecord(data);
423+
if (data.Position >= data.Length)
424+
break;
425+
426+
var record = ParseRelocationRecord(data);
427+
if (record == null)
428+
break;
429+
430+
obj.RelocationRecords[i] = record;
424431
}
425432

426433
return obj;
@@ -433,12 +440,20 @@ public static PerSegmentData ParsePerSegmentData(Stream data)
433440
/// <returns>Filled RelocationRecord on success, null on error</returns>
434441
public static RelocationRecord ParseRelocationRecord(Stream data)
435442
{
443+
// Handle partial relocation sections
444+
if (data.Position > data.Length - 4)
445+
return null;
446+
436447
var obj = new RelocationRecord();
437448

438449
obj.SourceType = (RelocationRecordSourceType)data.ReadByteValue();
439450
obj.Flags = (RelocationRecordFlag)data.ReadByteValue();
440451
obj.Offset = data.ReadUInt16LittleEndian();
441452

453+
// Handle incomplete entries
454+
if (data.Position > data.Length - 4)
455+
return obj;
456+
442457
switch (obj.Flags & RelocationRecordFlag.TARGET_MASK)
443458
{
444459
case RelocationRecordFlag.INTERNALREF:

SabreTools.Serialization/Deserializers/PortableExecutable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ public static void ParseResourceData(Stream data,
16011601
continue;
16021602

16031603
// If the offset is within the table data, read from there
1604-
if (nextOffset - tableStart + entry.DataEntry.Size <= tableLength)
1604+
if (nextOffset > tableStart && nextOffset - tableStart + entry.DataEntry.Size <= tableLength)
16051605
{
16061606
dataOffset = (int)(nextOffset - tableStart);
16071607
entry.DataEntry.Data = tableData.ReadBytes(ref dataOffset, (int)entry.DataEntry.Size);

SabreTools.Serialization/Extensions.PortableExecutable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ public static SecuROMAddDEntry ParseSecuROMAddDEntry(this byte[] data, ref int o
729729
#region Creation data
730730

731731
dialogItemTemplate.CreationDataSize = entry.Data.ReadUInt16LittleEndian(ref offset);
732-
if (dialogItemTemplate.CreationDataSize != 0)
732+
if (dialogItemTemplate.CreationDataSize != 0 && dialogItemTemplate.CreationDataSize + offset < entry.Data.Length)
733733
dialogItemTemplate.CreationData = entry.Data.ReadBytes(ref offset, dialogItemTemplate.CreationDataSize);
734734

735735
#endregion

0 commit comments

Comments
 (0)