diff --git a/include/llvm/Object/ELFTypes.h b/include/llvm/Object/ELFTypes.h index fafdcd7bdae..c1fadf8e210 100644 --- a/include/llvm/Object/ELFTypes.h +++ b/include/llvm/Object/ELFTypes.h @@ -642,14 +642,19 @@ class Elf_Note_Iterator_Impl // container, either cleanly or with an overflow error. void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { RemainingSize -= NoteSize; - if (RemainingSize == 0u) + if (RemainingSize == 0u) { + // Ensure that if the iterator walks to the end, the error is checked + // afterwards. + *Err = Error::success(); Nhdr = nullptr; - else if (sizeof(*Nhdr) > RemainingSize) + } else if (sizeof(*Nhdr) > RemainingSize) stopWithOverflowError(); else { Nhdr = reinterpret_cast *>(NhdrPos + NoteSize); if (Nhdr->getSize() > RemainingSize) stopWithOverflowError(); + else + *Err = Error::success(); } } @@ -657,6 +662,7 @@ class Elf_Note_Iterator_Impl explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {} Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, Error &Err) : RemainingSize(Size), Err(&Err) { + consumeError(std::move(Err)); assert(Start && "ELF note iterator starting at NULL"); advanceNhdr(Start, 0u); } @@ -670,6 +676,10 @@ public: return *this; } bool operator==(Elf_Note_Iterator_Impl Other) const { + if (!Nhdr) + (void)(bool)(*Other.Err); + if (!Other.Nhdr) + (void)(bool)(*Err); return Nhdr == Other.Nhdr; } bool operator!=(Elf_Note_Iterator_Impl Other) const { diff --git a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp index 3b9f62e9b5f..8a136de24a8 100644 --- a/tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ b/tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -123,14 +123,9 @@ findBuildID(const object::ELFFile &In) { if (Phdr.p_type != PT_NOTE) continue; Error Err = Error::success(); - if (Err) - llvm_unreachable("Error::success() was an error."); - for (const auto &Note : In.notes(Phdr, Err)) { - if (Err) - return std::move(Err); + for (const auto &Note : In.notes(Phdr, Err)) if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU) return Note.getDesc(); - } if (Err) return std::move(Err); }