Index: include/llvm/Object/Decompressor.h =================================================================== --- include/llvm/Object/Decompressor.h +++ include/llvm/Object/Decompressor.h @@ -13,6 +13,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Support/ErrorHandling.h" namespace llvm { namespace object { @@ -31,7 +32,9 @@ /// @brief Resize the buffer and uncompress section data into it. /// @param Out Destination buffer. template Error resizeAndDecompress(T &Out) { + install_bad_alloc_error_handler(outOfMemoryHandler, &DecompressedSize); Out.resize(DecompressedSize); + remove_bad_alloc_error_handler(); return decompress({Out.data(), (size_t)DecompressedSize}); } @@ -52,6 +55,8 @@ static bool isGnuStyle(StringRef Name); private: + static void outOfMemoryHandler(void *SizePtr, const std::string &Message, + bool); Decompressor(StringRef Data); Error consumeCompressedGnuHeader(); Index: lib/Object/Decompressor.cpp =================================================================== --- lib/Object/Decompressor.cpp +++ lib/Object/Decompressor.cpp @@ -92,3 +92,10 @@ size_t Size = Buffer.size(); return zlib::uncompress(SectionData, Buffer.data(), Size); } + +void Decompressor::outOfMemoryHandler(void *SizePtr, const std::string &Message, + bool) { + errs() << "Decompression failed: unable to allocate " + << *static_cast(SizePtr) << " bytes.\n"; + exit(1); +} Index: test/tools/llvm-dwarfdump/dwarf-invalid-compression.test =================================================================== --- /dev/null +++ test/tools/llvm-dwarfdump/dwarf-invalid-compression.test @@ -0,0 +1,2 @@ +RUN: not llvm-dwarfdump %p/Inputs/dwarf-invalid-compression 2>&1 | FileCheck %s +CHECK: Decompression failed: unable to allocate 2314885530818453536 bytes.