Skip to content

Commit 82da235

Browse files
max-charlambjkotasCopilot
authored
unify rva checks (#118936)
* unify rva checks * remove comment Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 1f8858c commit 82da235

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/coreclr/utilcode/pedecoder.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,6 @@ CHECK PEDecoder::CheckRva(RVA rva, COUNT_T size, int forbiddenFlags, IsNullOK ok
497497
CHECK(section != NULL);
498498

499499
CHECK(CheckBounds(VAL32(section->VirtualAddress),
500-
// AlignUp((UINT)VAL32(section->Misc.VirtualSize), (UINT)VAL32(FindNTHeaders()->OptionalHeader.SectionAlignment)),
501500
(UINT)VAL32(section->Misc.VirtualSize),
502501
rva, size));
503502
if(!IsMapped())
@@ -776,9 +775,17 @@ IMAGE_SECTION_HEADER *PEDecoder::RvaToSection(RVA rva) const
776775

777776
while (section < sectionEnd)
778777
{
779-
if (rva < (VAL32(section->VirtualAddress)
780-
+ AlignUp((UINT)VAL32(section->Misc.VirtualSize), (UINT)VAL32(FindNTHeaders()->OptionalHeader.SectionAlignment))))
778+
// The RVA should be within a section's virtual address range.
779+
if (rva < (VAL32(section->VirtualAddress) + VAL32(section->Misc.VirtualSize)))
781780
{
781+
if (!IsMapped())
782+
{
783+
// On flat images (!IsMapped()), the RVA should also be within the section's raw data range.
784+
if (rva >= (VAL32(section->VirtualAddress) + VAL32(section->SizeOfRawData)))
785+
{
786+
return NULL;
787+
}
788+
}
782789
if (rva < VAL32(section->VirtualAddress))
783790
RETURN NULL;
784791
else
@@ -847,7 +854,6 @@ TADDR PEDecoder::GetRvaData(RVA rva, IsNullOK ok /*= NULL_NOT_OK*/) const
847854
offset = rva;
848855
else
849856
{
850-
// !!! check for case where rva is in padded portion of segment
851857
offset = RvaToOffset(rva);
852858
}
853859

0 commit comments

Comments
 (0)