diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index a824db5f34d..dc7c56c98ac 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -207,7 +207,10 @@ static std::error_code parseSegmentLoadCommand( const unsigned SegmentLoadSize = sizeof(SegmentCmd); if (Load.C.cmdsize < SegmentLoadSize) return object_error::macho_load_segment_too_small; - SegmentCmd S = getStruct(Obj, Load.Ptr); + auto SegOrErr = getStructOrErr(Obj, Load.Ptr); + if (!SegOrErr) + return SegOrErr.getError(); + SegmentCmd S = SegOrErr.get(); const unsigned SectionSize = Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section); if (S.nsects > std::numeric_limits::max() / SectionSize || diff --git a/test/Object/Inputs/macho64-invalid-incomplete-segment-load-command b/test/Object/Inputs/macho64-invalid-incomplete-segment-load-command new file mode 100644 index 00000000000..82ec7246bdb Binary files /dev/null and b/test/Object/Inputs/macho64-invalid-incomplete-segment-load-command differ diff --git a/test/Object/macho-invalid.test b/test/Object/macho-invalid.test index 8bc11508dd5..f4aa1e0c298 100644 --- a/test/Object/macho-invalid.test +++ b/test/Object/macho-invalid.test @@ -37,3 +37,6 @@ INVALID-SECTION-IDX-SYMBOL-SEC: getSymbolSection: Invalid section index RUN: not llvm-objdump -private-headers %p/Inputs/macho-invalid-header 2>&1 | FileCheck -check-prefix INVALID-HEADER %s INVALID-HEADER: Invalid data was encountered while parsing the file + +RUN: not llvm-objdump -private-headers %p/Inputs/macho64-invalid-incomplete-segment-load-command 2>&1 | FileCheck -check-prefix INCOMPLETE-SEGMENT-LOADC %s +INCOMPLETE-SEGMENT-LOADC: Invalid data was encountered while parsing the file