Index: include/llvm/Object/ELFTypes.h =================================================================== --- include/llvm/Object/ELFTypes.h +++ include/llvm/Object/ELFTypes.h @@ -631,6 +631,7 @@ // Stop iteration and indicate an overflow. void stopWithOverflowError() { Nhdr = nullptr; + ErrorAsOutParameter EAOP(Err); *Err = make_error("ELF note overflows container", object_error::parse_failed); } @@ -642,9 +643,12 @@ // 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); @@ -657,6 +661,7 @@ 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); } Index: tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -123,14 +123,9 @@ 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); }