Skip to content

Commit 74bd988

Browse files
committed
Migrate Binary::checkOffset from error_code to Error, NFC
In my use case, this saved 100ms of time doing one-time-initialization for std::error_code().
1 parent 5d62606 commit 74bd988

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

llvm/include/llvm/Object/Binary.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,14 @@ class Binary {
160160
return Triple::UnknownObjectFormat;
161161
}
162162

163-
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
164-
const uint64_t Size) {
163+
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
164+
const uint64_t Size) {
165165
if (Addr + Size < Addr || Addr + Size < Size ||
166166
Addr + Size > uintptr_t(M.getBufferEnd()) ||
167167
Addr < uintptr_t(M.getBufferStart())) {
168-
return object_error::unexpected_eof;
168+
return errorCodeToError(object_error::unexpected_eof);
169169
}
170-
return std::error_code();
170+
return Error::success();
171171
}
172172
};
173173

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,10 +744,10 @@ ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec) const {
744744
const Elf_Shdr *EShdr = getSection(Sec);
745745
if (EShdr->sh_type == ELF::SHT_NOBITS)
746746
return makeArrayRef((const uint8_t *)base(), 0);
747-
if (std::error_code EC =
747+
if (Error E =
748748
checkOffset(getMemoryBufferRef(),
749749
(uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
750-
return errorCodeToError(EC);
750+
return std::move(E);
751751
return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
752752
EShdr->sh_size);
753753
}

llvm/lib/Object/COFFObjectFile.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
5959
const void *Ptr,
6060
const uint64_t Size = sizeof(T)) {
6161
uintptr_t Addr = uintptr_t(Ptr);
62-
if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
63-
return EC;
62+
if (Error E = Binary::checkOffset(M, Addr, Size))
63+
return errorToErrorCode(std::move(E));
6464
Obj = reinterpret_cast<const T *>(Addr);
6565
return std::error_code();
6666
}
@@ -374,9 +374,11 @@ getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
374374
// relocations.
375375
begin++;
376376
}
377-
if (Binary::checkOffset(M, uintptr_t(begin),
378-
sizeof(coff_relocation) * NumRelocs))
377+
if (auto E = Binary::checkOffset(M, uintptr_t(begin),
378+
sizeof(coff_relocation) * NumRelocs)) {
379+
consumeError(std::move(E));
379380
return nullptr;
381+
}
380382
return begin;
381383
}
382384

@@ -555,8 +557,8 @@ std::error_code COFFObjectFile::initImportTablePtr() {
555557
uintptr_t IntPtr = 0;
556558
if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
557559
return EC;
558-
if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
559-
return EC;
560+
if (Error E = checkOffset(Data, IntPtr, DataEntry->Size))
561+
return errorToErrorCode(std::move(E));
560562
ImportDirectory = reinterpret_cast<
561563
const coff_import_directory_table_entry *>(IntPtr);
562564
return std::error_code();
@@ -1093,8 +1095,8 @@ Error COFFObjectFile::getSectionContents(const coff_section *Sec,
10931095
// data, as there's nothing that says that is not allowed.
10941096
uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
10951097
uint32_t SectionSize = getSectionSize(Sec);
1096-
if (checkOffset(Data, ConStart, SectionSize))
1097-
return make_error<BinaryError>();
1098+
if (Error E = checkOffset(Data, ConStart, SectionSize))
1099+
return E;
10981100
Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
10991101
return Error::success();
11001102
}

llvm/lib/Object/XCOFFObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ template <typename T>
2929
static Expected<const T *> getObject(MemoryBufferRef M, const void *Ptr,
3030
const uint64_t Size = sizeof(T)) {
3131
uintptr_t Addr = uintptr_t(Ptr);
32-
if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
33-
return errorCodeToError(EC);
32+
if (Error E = Binary::checkOffset(M, Addr, Size))
33+
return std::move(E);
3434
return reinterpret_cast<const T *>(Addr);
3535
}
3636

0 commit comments

Comments
 (0)