Don't loop endlessly for MachO files with 0 ncmds

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225271 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Filipe Cabecinhas 2015-01-06 17:08:26 +00:00
parent ed4e93d5ba
commit d682839830
4 changed files with 14 additions and 0 deletions

View 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;

Binary file not shown.

View File

@ -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

View 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) {