Skip to content

Commit d2e1626

Browse files
committed
Fix reading elf section from file buffer
1 parent f74502e commit d2e1626

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ class ReflectionContext
479479
for (const typename T::Section *Hdr : SecHdrVec) {
480480
uint32_t Offset = Hdr->sh_name;
481481
const char *Start = (const char *)StrTab + Offset;
482-
uint64_t Size = strnlen(Start, StrTabSize - Offset);
483-
if (Size > StrTabSize - Offset) {
482+
uint64_t StringSize = strnlen(Start, StrTabSize - Offset);
483+
if (StringSize > StrTabSize - Offset) {
484484
Error = true;
485485
break;
486486
}
487-
std::string SecName(Start, Size);
487+
std::string SecName(Start, StringSize);
488488
if (SecName != Name)
489489
continue;
490490
RemoteAddress SecStart =
@@ -495,15 +495,16 @@ class ReflectionContext
495495
// sh_offset gives us the offset to the section in the file,
496496
// while sh_addr gives us the offset in the process.
497497
auto Offset = Hdr->sh_offset;
498-
if (FileBuffer->allocatedSize() > Offset + Size) {
498+
if (FileBuffer->allocatedSize() < Offset + SecSize) {
499499
Error = true;
500500
break;
501501
}
502502
auto *Buf = malloc(SecSize);
503503
SecBuf = MemoryReader::ReadBytesResult(
504504
Buf, [](const void *ptr) { free(const_cast<void *>(ptr)); });
505505
memcpy((void *)Buf,
506-
(const void *)((uint64_t)FileBuffer->base() + Offset), Size);
506+
(const void *)((uint64_t)FileBuffer->base() + Offset),
507+
SecSize);
507508
} else {
508509
SecBuf = this->getReader().readBytes(SecStart, SecSize);
509510
}

0 commit comments

Comments
 (0)