From 1051909df1a2dfc9e3b4b27d15b36b3019c21463 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Mon, 27 Jun 2016 21:39:39 +0000 Subject: Change all but the last ErrorOr<...> use for MachOUniversalBinary to Expected<...> to allow a good error message to be produced. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I added the one test case that the object file tools could produce an error message. The other two errors can’t be triggered if the input file is passed through sys::fs::identify_magic(). But the malformedError("bad magic number") does get triggered by the logic in llvm-dsymutil when dealing with a normal Mach-O file. The other "File too small ..." error would take a logic error currently to produce and is not tested for. llvm-svn: 273946 --- llvm/lib/Object/MachOUniversal.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Object/MachOUniversal.cpp') diff --git a/llvm/lib/Object/MachOUniversal.cpp b/llvm/lib/Object/MachOUniversal.cpp index b3b0c251c10..e6880587cde 100644 --- a/llvm/lib/Object/MachOUniversal.cpp +++ b/llvm/lib/Object/MachOUniversal.cpp @@ -22,6 +22,13 @@ using namespace llvm; using namespace object; +static Error +malformedError(Twine Msg) { + std::string StringMsg = "truncated or malformed fat file (" + Msg.str() + ")"; + return make_error(std::move(StringMsg), + object_error::parse_failed); +} + template static T getUniversalBinaryStruct(const char *Ptr) { T Res; @@ -92,22 +99,24 @@ MachOUniversalBinary::ObjectForArch::getAsArchive() const { void MachOUniversalBinary::anchor() { } -ErrorOr> +Expected> MachOUniversalBinary::create(MemoryBufferRef Source) { - std::error_code EC; + Error Err; std::unique_ptr Ret( - new MachOUniversalBinary(Source, EC)); - if (EC) - return EC; + new MachOUniversalBinary(Source, Err)); + if (Err) + return std::move(Err); return std::move(Ret); } -MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, - std::error_code &ec) +MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err) : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0), NumberOfObjects(0) { + ErrorAsOutParameter ErrAsOutParam(Err); if (Data.getBufferSize() < sizeof(MachO::fat_header)) { - ec = object_error::invalid_file_type; + Err = make_error("File too small to be a Mach-O " + "universal file", + object_error::invalid_file_type); return; } // Check for magic value and sufficient header size. @@ -121,14 +130,16 @@ MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, else if (Magic == MachO::FAT_MAGIC_64) MinSize += sizeof(MachO::fat_arch_64) * NumberOfObjects; else { - ec = object_error::parse_failed; + Err = malformedError("bad magic number"); return; } if (Buf.size() < MinSize) { - ec = object_error::parse_failed; + Err = malformedError("fat_arch" + + Twine(Magic == MachO::FAT_MAGIC ? "" : "_64") + + " structs would extend past the end of the file"); return; } - ec = std::error_code(); + Err = Error::success(); } Expected> -- cgit v1.2.3