From b34e3a1877fb095b0386c7e3675aca66ff26edeb Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Thu, 5 May 2016 17:43:35 +0000 Subject: Clean up the specific error message for a malformed Mach-O files with bad segment load commands. The existing test case in test/Object/macho-invalid.test for macho-invalid-too-small-segment-load-command has a cmdsize of 55, while being too small also it is not a multiple of 4. So when that check is added this test case will produce a different error. So I constructed a new test case that will trigger the intended error. I also changed the error message to be consistent with the other malformed Mach-O file error messages which prints the load command index. I also removed both object_error::macho_load_segment_too_small and object_error::macho_load_segment_too_many_sections from Object/Error.h as they are not needed and can just use object_error::parse_failed and let the error message string distinguish the specific error. llvm-svn: 268652 --- llvm/lib/Object/MachOObjectFile.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'llvm/lib/Object/MachOObjectFile.cpp') diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp index ad29d00..516607d 100644 --- a/llvm/lib/Object/MachOObjectFile.cpp +++ b/llvm/lib/Object/MachOObjectFile.cpp @@ -234,22 +234,23 @@ static void parseHeader(const MachOObjectFile *Obj, T &Header, template static Error parseSegmentLoadCommand( const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load, - SmallVectorImpl &Sections, bool &IsPageZeroSegment) { + SmallVectorImpl &Sections, bool &IsPageZeroSegment, + uint32_t LoadCommandIndex, const char *CmdName) { const unsigned SegmentLoadSize = sizeof(SegmentCmd); if (Load.C.cmdsize < SegmentLoadSize) - return malformedError(*Obj, - "Mach-O segment load command size is too small", - object_error::macho_load_segment_too_small); + return malformedError(*Obj, Twine("truncated or malformed object " + "(load command ") + Twine(LoadCommandIndex) + + Twine(" ") + CmdName + Twine(" cmdsize too small)")); if (auto SegOrErr = getStructOrErr(Obj, Load.Ptr)) { SegmentCmd S = SegOrErr.get(); const unsigned SectionSize = Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section); if (S.nsects > std::numeric_limits::max() / SectionSize || S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize) - return malformedError(*Obj, - "Mach-O segment load command contains too many " - "sections", - object_error::macho_load_segment_too_many_sections); + return malformedError(*Obj, Twine("truncated or malformed object " + "(load command ") + Twine(LoadCommandIndex) + + Twine(" inconsistent cmdsize in ") + CmdName + + Twine(" for the number of sections)")); for (unsigned J = 0; J < S.nsects; ++J) { const char *Sec = getSectionPtr(Obj, Load, J); Sections.push_back(Sec); @@ -357,11 +358,12 @@ MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, UuidLoadCmd = Load.Ptr; } else if (Load.C.cmd == MachO::LC_SEGMENT_64) { if ((Err = parseSegmentLoadCommand( - this, Load, Sections, HasPageZeroSegment))) + this, Load, Sections, HasPageZeroSegment, I, + "LC_SEGMENT_64"))) return; } else if (Load.C.cmd == MachO::LC_SEGMENT) { if ((Err = parseSegmentLoadCommand( - this, Load, Sections, HasPageZeroSegment))) + this, Load, Sections, HasPageZeroSegment, I, "LC_SEGMENT"))) return; } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB || Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB || -- cgit v1.1