diff --git a/src/coreclr/utilcode/pedecoder.cpp b/src/coreclr/utilcode/pedecoder.cpp index 338c0fc3e07e89..810912db910d76 100644 --- a/src/coreclr/utilcode/pedecoder.cpp +++ b/src/coreclr/utilcode/pedecoder.cpp @@ -497,7 +497,6 @@ CHECK PEDecoder::CheckRva(RVA rva, COUNT_T size, int forbiddenFlags, IsNullOK ok CHECK(section != NULL); CHECK(CheckBounds(VAL32(section->VirtualAddress), - // AlignUp((UINT)VAL32(section->Misc.VirtualSize), (UINT)VAL32(FindNTHeaders()->OptionalHeader.SectionAlignment)), (UINT)VAL32(section->Misc.VirtualSize), rva, size)); if(!IsMapped()) @@ -776,9 +775,17 @@ IMAGE_SECTION_HEADER *PEDecoder::RvaToSection(RVA rva) const while (section < sectionEnd) { - if (rva < (VAL32(section->VirtualAddress) - + AlignUp((UINT)VAL32(section->Misc.VirtualSize), (UINT)VAL32(FindNTHeaders()->OptionalHeader.SectionAlignment)))) + // The RVA should be within a section's virtual address range. + if (rva < (VAL32(section->VirtualAddress) + VAL32(section->Misc.VirtualSize))) { + if (!IsMapped()) + { + // On flat images (!IsMapped()), the RVA should also be within the section's raw data range. + if (rva >= (VAL32(section->VirtualAddress) + VAL32(section->SizeOfRawData))) + { + return NULL; + } + } if (rva < VAL32(section->VirtualAddress)) RETURN NULL; else @@ -847,7 +854,6 @@ TADDR PEDecoder::GetRvaData(RVA rva, IsNullOK ok /*= NULL_NOT_OK*/) const offset = rva; else { - // !!! check for case where rva is in padded portion of segment offset = RvaToOffset(rva); }