r279722 - Fix memory leaks in clang-offload-bundler

Vitaly Buka via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 25 00:21:35 PDT 2016


Author: vitalybuka
Date: Thu Aug 25 02:21:34 2016
New Revision: 279722

URL: http://llvm.org/viewvc/llvm-project?rev=279722&view=rev
Log:
Fix memory leaks in clang-offload-bundler

Summary:
1. Pair removed from StringMap was not destroyed
2. ObjectFile had no owner

Reviewers: sfantao

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D23865

Modified:
    cfe/trunk/tools/clang-offload-bundler/ClangOffloadBundler.cpp

Modified: cfe/trunk/tools/clang-offload-bundler/ClangOffloadBundler.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-offload-bundler/ClangOffloadBundler.cpp?rev=279722&r1=279721&r2=279722&view=diff
==============================================================================
--- cfe/trunk/tools/clang-offload-bundler/ClangOffloadBundler.cpp (original)
+++ cfe/trunk/tools/clang-offload-bundler/ClangOffloadBundler.cpp Thu Aug 25 02:21:34 2016
@@ -348,10 +348,10 @@ public:
 class ObjectFileHandler final : public FileHandler {
 
   /// The object file we are currently dealing with.
-  ObjectFile &Obj;
+  std::unique_ptr<ObjectFile> Obj;
 
   /// Return the input file contents.
-  StringRef getInputFileContents() const { return Obj.getData(); }
+  StringRef getInputFileContents() const { return Obj->getData(); }
 
   /// Return true if the provided section is an offload section and return the
   /// triple by reference.
@@ -400,7 +400,7 @@ public:
   void ReadHeader(MemoryBuffer &Input) {}
   StringRef ReadBundleStart(MemoryBuffer &Input) {
 
-    while (NextSection != Obj.section_end()) {
+    while (NextSection != Obj->section_end()) {
       CurrentSection = NextSection;
       ++NextSection;
 
@@ -561,9 +561,10 @@ public:
     GV->setSection(SectionName);
   }
 
-  ObjectFileHandler(ObjectFile &Obj)
-      : FileHandler(), Obj(Obj), CurrentSection(Obj.section_begin()),
-        NextSection(Obj.section_begin()) {}
+  ObjectFileHandler(std::unique_ptr<ObjectFile> ObjIn)
+      : FileHandler(), Obj(std::move(ObjIn)),
+        CurrentSection(Obj->section_begin()),
+        NextSection(Obj->section_begin()) {}
   ~ObjectFileHandler() {}
 };
 
@@ -677,12 +678,13 @@ static FileHandler *CreateObjectFileHand
   // 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.
-  ObjectFile *Obj = dyn_cast<ObjectFile>(BinaryOrErr.get().release());
+  std::unique_ptr<ObjectFile> Obj(
+      dyn_cast<ObjectFile>(BinaryOrErr.get().release()));
 
   if (!Obj)
     return new BinaryFileHandler();
 
-  return new ObjectFileHandler(*Obj);
+  return new ObjectFileHandler(std::move(Obj));
 }
 
 /// Return an appropriate handler given the input files and options.
@@ -821,7 +823,7 @@ static bool UnbundleFiles() {
     }
     FH.get()->ReadBundle(OutputFile, Input);
     FH.get()->ReadBundleEnd(Input);
-    Worklist.remove(&*Output);
+    Worklist.erase(Output);
 
     // Record if we found the host bundle.
     if (hasHostKind(CurTriple))




More information about the cfe-commits mailing list