Index: include/llvm/Object/ELF.h =================================================================== --- include/llvm/Object/ELF.h +++ include/llvm/Object/ELF.h @@ -226,7 +226,8 @@ if (Size % sizeof(T)) return object_error::parse_failed; - if (Offset + Size > Buf.size()) + if ((std::numeric_limits::max() - Offset < Size) || + Offset + Size > Buf.size()) return object_error::parse_failed; const T *Start = reinterpret_cast(base() + Offset); Index: test/Object/invalid.test =================================================================== --- test/Object/invalid.test +++ test/Object/invalid.test @@ -64,3 +64,7 @@ RUN: not llvm-readobj -r %p/Inputs/invalid-relocation-sec-sh_offset.elf-x86-64 2>&1 | \ RUN: FileCheck --check-prefix=INVALID-RELOC-SH-OFFSET %s INVALID-RELOC-SH-OFFSET: Invalid data was encountered while parsing the file + +RUN: not llvm-readobj -t %p/Inputs/invalid-section-size2.elf 2>&1 | \ +RUN: FileCheck --check-prefix=INVALID-SECTION-SIZE2 %s +INVALID-SECTION-SIZE2: Invalid data was encountered while parsing the file.