Don't loop endlessly for MachO files with 0 ncmds
authorFilipe Cabecinhas <me@filcab.net>
Tue, 6 Jan 2015 17:08:26 +0000 (17:08 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Tue, 6 Jan 2015 17:08:26 +0000 (17:08 +0000)
llvm-svn: 225271

llvm/lib/Object/MachOObjectFile.cpp
llvm/test/Object/Inputs/macho-zero-ncmds [new file with mode: 0644]
llvm/test/Object/objdump-macho-quirks.test [new file with mode: 0644]
llvm/tools/llvm-objdump/MachODump.cpp

index 1373dba..e476976 100644 (file)
@@ -236,6 +236,9 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
       DataInCodeLoadCmd(nullptr), DyldInfoLoadCmd(nullptr),
       UuidLoadCmd(nullptr), HasPageZeroSegment(false) {
   uint32_t LoadCommandCount = this->getHeader().ncmds;
+  if (LoadCommandCount == 0)
+    return;
+
   MachO::LoadCommandType SegmentLoadType = is64Bit() ?
     MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
 
diff --git a/llvm/test/Object/Inputs/macho-zero-ncmds b/llvm/test/Object/Inputs/macho-zero-ncmds
new file mode 100644 (file)
index 0000000..0505419
Binary files /dev/null and b/llvm/test/Object/Inputs/macho-zero-ncmds differ
diff --git a/llvm/test/Object/objdump-macho-quirks.test b/llvm/test/Object/objdump-macho-quirks.test
new file mode 100644 (file)
index 0000000..eeee153
--- /dev/null
@@ -0,0 +1,9 @@
+RUN: llvm-objdump -private-headers %p/Inputs/macho-zero-ncmds \
+RUN:         | FileCheck %s -check-prefix A
+
+// Check that we don't get an infinite loop if ncmds = 0
+A: file format Mach-O 64-bit unknown
+A: Mach header
+A:      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flags
+A: MH_MAGIC_64  0x00      OBJECT     0          0 0x00000000
+
index 36db9ad..935696b 100644 (file)
@@ -4204,6 +4204,8 @@ static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
                               uint32_t filetype, uint32_t cputype,
                               bool verbose) {
+  if (ncmds == 0)
+    return;
   StringRef Buf = Obj->getData();
   MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
   for (unsigned i = 0;; ++i) {