From 0c1257f517e00f67c9fbb969671e85509a7f8ac9 Mon Sep 17 00:00:00 2001 From: Sergey Dmitriev Date: Wed, 11 Sep 2019 16:03:21 +0000 Subject: [PATCH] [Clang][Bundler] Fix for a potential memory leak [NFC] Bundler leaks memory if it is called with -type=o but given input isn't an object file (though it has to have a known binary type like IR, archive, etc...). Memory leak is happening when binary object returned by the createBinary(...) call cannot be casted to an ObjectFile type. In this case returned BinaryOrErr object releases ownership of the binary, but no one is taking it (see line 626). Differential Revision: https://reviews.llvm.org/D67416 llvm-svn: 371633 --- .../ClangOffloadBundler.cpp | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp b/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp index fcc6a9898526..1614abb54e2f 100644 --- a/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp +++ b/clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp @@ -611,24 +611,15 @@ static FileHandler *CreateObjectFileHandler(MemoryBuffer &FirstInput) { // Check if the input file format is one that we know how to deal with. Expected> BinaryOrErr = createBinary(FirstInput); - // Failed to open the input as a known binary. Use the default binary handler. - if (!BinaryOrErr) { - // We don't really care about the error (we just consume it), if we could - // not get a valid device binary object we use the default binary handler. - consumeError(BinaryOrErr.takeError()); - return new BinaryFileHandler(); - } - - // We only support regular object files. If this is not an object file, - // default to the binary handler. The handler will be owned by the client of - // this function. - std::unique_ptr Obj( - dyn_cast(BinaryOrErr.get().release())); - - if (!Obj) + // We only support regular object files. If failed to open the input as a + // known binary or this is not an object file use the default binary handler. + if (errorToBool(BinaryOrErr.takeError()) || !isa(*BinaryOrErr)) return new BinaryFileHandler(); - return new ObjectFileHandler(std::move(Obj)); + // Otherwise create an object file handler. The handler will be owned by the + // client of this function. + return new ObjectFileHandler( + std::unique_ptr(cast(BinaryOrErr->release()))); } /// Return an appropriate handler given the input files and options.