Skip to content

Commit 0c1257f

Browse files
committed
[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
1 parent 1be6340 commit 0c1257f

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

clang/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -611,24 +611,15 @@ static FileHandler *CreateObjectFileHandler(MemoryBuffer &FirstInput) {
611611
// Check if the input file format is one that we know how to deal with.
612612
Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(FirstInput);
613613

614-
// Failed to open the input as a known binary. Use the default binary handler.
615-
if (!BinaryOrErr) {
616-
// We don't really care about the error (we just consume it), if we could
617-
// not get a valid device binary object we use the default binary handler.
618-
consumeError(BinaryOrErr.takeError());
619-
return new BinaryFileHandler();
620-
}
621-
622-
// We only support regular object files. If this is not an object file,
623-
// default to the binary handler. The handler will be owned by the client of
624-
// this function.
625-
std::unique_ptr<ObjectFile> Obj(
626-
dyn_cast<ObjectFile>(BinaryOrErr.get().release()));
627-
628-
if (!Obj)
614+
// We only support regular object files. If failed to open the input as a
615+
// known binary or this is not an object file use the default binary handler.
616+
if (errorToBool(BinaryOrErr.takeError()) || !isa<ObjectFile>(*BinaryOrErr))
629617
return new BinaryFileHandler();
630618

631-
return new ObjectFileHandler(std::move(Obj));
619+
// Otherwise create an object file handler. The handler will be owned by the
620+
// client of this function.
621+
return new ObjectFileHandler(
622+
std::unique_ptr<ObjectFile>(cast<ObjectFile>(BinaryOrErr->release())));
632623
}
633624

634625
/// Return an appropriate handler given the input files and options.

0 commit comments

Comments
 (0)