Skip to content

Commit 1ebfd4e

Browse files
committed
Fix incorrect and O(n^2) check for overlapping sections (triggered in the new ManySections test).
1 parent f51d461 commit 1ebfd4e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Diff for: src/LibObjectFile/Elf/ElfReader{TDecoder}.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ private void VerifyAndFixProgramHeadersAndSections()
512512
// Make sure to pre-sort all sections by offset
513513
var orderedSections = new List<ElfSection>(ObjectFile.Sections.Count);
514514
orderedSections.AddRange(ObjectFile.Sections);
515-
orderedSections.Sort(CompareSectionOffsetsDelegate);
515+
orderedSections.Sort(CompareSectionOffsetsAndSizesDelegate);
516516
// Store the stream index to recover the same order when saving back.
517517
for(int i = 0; i < orderedSections.Count; i++)
518518
{
@@ -548,10 +548,10 @@ private void VerifyAndFixProgramHeadersAndSections()
548548
lastOffset = section.Offset + section.Size - 1;
549549

550550
// Verify overlapping sections and generate and error
551-
for (int j = i + 1; j < orderedSections.Count; j++)
551+
if (i + 1 < orderedSections.Count)
552552
{
553-
var otherSection = orderedSections[j];
554-
if (section.Contains(otherSection) || otherSection.Contains(section))
553+
var otherSection = orderedSections[i + 1];
554+
if (otherSection.Offset < section.Offset + section.Size)
555555
{
556556
Diagnostics.Warning(DiagnosticId.ELF_ERR_InvalidOverlappingSections, $"The section {section} [{section.Offset} : {section.Offset + section.Size - 1}] is overlapping with the section {otherSection} [{otherSection.Offset} : {otherSection.Offset + otherSection.Size - 1}]");
557557
}
@@ -844,11 +844,16 @@ public override ushort Decode(ElfNative.Elf64_Versym src)
844844
return _decoder.Decode(src);
845845
}
846846

847-
private static readonly Comparison<ElfSection> CompareSectionOffsetsDelegate = new Comparison<ElfSection>(CompareSectionOffsets);
847+
private static readonly Comparison<ElfSection> CompareSectionOffsetsAndSizesDelegate = new Comparison<ElfSection>(CompareSectionOffsetsAndSizes);
848848

849-
private static int CompareSectionOffsets(ElfSection left, ElfSection right)
849+
private static int CompareSectionOffsetsAndSizes(ElfSection left, ElfSection right)
850850
{
851-
return left.Offset.CompareTo(right.Offset);
851+
int result = left.Offset.CompareTo(right.Offset);
852+
if (result == 0)
853+
{
854+
result = left.Size.CompareTo(right.Size);
855+
}
856+
return result;
852857
}
853858
}
854859
}

Diff for: src/LibObjectFile/Elf/Sections/ElfSymbolTableSectionHeaderIndices.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Reflection.PortableExecutable;
87

98
namespace LibObjectFile.Elf
109
{

0 commit comments

Comments
 (0)