[Object, MachO] Don't crash on incomplete MachO segment load commands.
authorAlexey Samsonov <vonosmas@gmail.com>
Thu, 4 Jun 2015 22:26:44 +0000 (22:26 +0000)
committerAlexey Samsonov <vonosmas@gmail.com>
Thu, 4 Jun 2015 22:26:44 +0000 (22:26 +0000)
Report proper error code from MachOObjectFile constructor if we
can't parse another segment load command (we already return a proper
error if segment load command contents is suspicious).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239109 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/MachOObjectFile.cpp
test/Object/Inputs/macho64-invalid-incomplete-segment-load-command [new file with mode: 0644]
test/Object/macho-invalid.test

index a824db5f34d17a054e77557ff433c4b9f70776f1..dc7c56c98ac54b364cc30bdfce91beb4e26195f0 100644 (file)
@@ -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<SegmentCmd>(Obj, Load.Ptr);
+  auto SegOrErr = getStructOrErr<SegmentCmd>(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<uint32_t>::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 (file)
index 0000000..82ec724
Binary files /dev/null and b/test/Object/Inputs/macho64-invalid-incomplete-segment-load-command differ
index 8bc11508dd59c2a91b03a66e98ebb6337c69b151..f4aa1e0c2984b3e631453e61737c9881a428c03d 100644 (file)
@@ -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