diff options
author | Alexey Samsonov <vonosmas@gmail.com> | 2015-06-04 20:08:52 +0000 |
---|---|---|
committer | Alexey Samsonov <vonosmas@gmail.com> | 2015-06-04 20:08:52 +0000 |
commit | 074da9b5e7201ceaed997a1ff2eab4464b62da4c (patch) | |
tree | f572c7fd89e7aaedd3f646d74275ae09beadcb5d /llvm/lib/Object/MachOObjectFile.cpp | |
parent | de5a94a6b4d1574fea0f252e65408343338ba664 (diff) | |
download | llvm-074da9b5e7201ceaed997a1ff2eab4464b62da4c.zip llvm-074da9b5e7201ceaed997a1ff2eab4464b62da4c.tar.gz llvm-074da9b5e7201ceaed997a1ff2eab4464b62da4c.tar.bz2 |
[Object, MachO] Don't crash on invalid MachO segment load commands.
Summary:
Properly report the error in segment load commands from MachOObjectFile
constructor instead of crashing the program.
Adjust the test case accordingly.
Test Plan: regression test suite
Reviewers: rafael, filcab
Subscribers: llvm-commits
llvm-svn: 239081
Diffstat (limited to 'llvm/lib/Object/MachOObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/MachOObjectFile.cpp | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index 3262c6c..62514d2 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -66,17 +66,16 @@ static ErrorOr<T> getStructOrErr(const MachOObjectFile *O, const char *P) { } template <typename SegmentCmd> -static uint32_t getSegmentLoadCommandNumSections(const SegmentCmd &S, - uint32_t Cmdsize) { +static ErrorOr<uint32_t> getSegmentLoadCommandNumSections(const SegmentCmd &S, + uint32_t Cmdsize) { const unsigned SectionSize = sizeof(SegmentCmd); if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize || S.nsects * SectionSize > Cmdsize - sizeof(S)) - report_fatal_error( - "Number of sections too large for size of load command."); + return object_error::macho_load_segment_too_many_sections; return S.nsects; } -static uint32_t +static ErrorOr<uint32_t> getSegmentLoadCommandNumSections(const MachOObjectFile *O, const MachOObjectFile::LoadCommandInfo &L) { if (O->is64Bit()) @@ -306,14 +305,19 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, } UuidLoadCmd = Load.Ptr; } else if (Load.C.cmd == SegmentLoadType) { - const unsigned SegmentLoadSize = this->is64Bit() + const unsigned SegmentLoadSize = is64Bit() ? sizeof(MachO::segment_command_64) : sizeof(MachO::segment_command); - if (Load.C.cmdsize < SegmentLoadSize) - report_fatal_error("Segment load command size is too small."); - - uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load); - for (unsigned J = 0; J < NumSections; ++J) { + if (Load.C.cmdsize < SegmentLoadSize) { + EC = object_error::macho_load_segment_too_small; + return; + } + auto NumSectionsOrErr = getSegmentLoadCommandNumSections(this, Load); + if (!NumSectionsOrErr) { + EC = NumSectionsOrErr.getError(); + return; + } + for (unsigned J = 0; J < NumSectionsOrErr.get(); ++J) { const char *Sec = getSectionPtr(this, Load, J); Sections.push_back(Sec); } |