Fznamznon created this revision.
Herald added a project: All.
Fznamznon requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Since each `DumpModuleInfoAction` can now contain a pointer to a
`raw_ostream`, saving there a poiter that owned by a local `unique_ptr`
may cause use-after-free.
Found by static analyzer.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146412

Files:
  clang/lib/Frontend/FrontendActions.cpp


Index: clang/lib/Frontend/FrontendActions.cpp
===================================================================
--- clang/lib/Frontend/FrontendActions.cpp
+++ clang/lib/Frontend/FrontendActions.cpp
@@ -781,13 +781,16 @@
   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
   CompilerInstance &CI = getCompilerInstance();
   StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
+  llvm::raw_ostream *POut = &llvm::outs();
   if (!OutputFileName.empty() && OutputFileName != "-") {
     std::error_code EC;
     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
                                            llvm::sys::fs::OF_TextWithCRLF));
-    OutputStream = OutFile.get();
+    POut = OutFile.get();
+  } else if (OutputStream) {
+    POut = OutputStream;
   }
-  llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();
+  llvm::raw_ostream &Out = *POut;
 
   Out << "Information for module file '" << getCurrentFile() << "':\n";
   auto &FileMgr = CI.getFileManager();


Index: clang/lib/Frontend/FrontendActions.cpp
===================================================================
--- clang/lib/Frontend/FrontendActions.cpp
+++ clang/lib/Frontend/FrontendActions.cpp
@@ -781,13 +781,16 @@
   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
   CompilerInstance &CI = getCompilerInstance();
   StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
+  llvm::raw_ostream *POut = &llvm::outs();
   if (!OutputFileName.empty() && OutputFileName != "-") {
     std::error_code EC;
     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
                                            llvm::sys::fs::OF_TextWithCRLF));
-    OutputStream = OutFile.get();
+    POut = OutFile.get();
+  } else if (OutputStream) {
+    POut = OutputStream;
   }
-  llvm::raw_ostream &Out = OutputStream ? *OutputStream : llvm::outs();
+  llvm::raw_ostream &Out = *POut;
 
   Out << "Information for module file '" << getCurrentFile() << "':\n";
   auto &FileMgr = CI.getFileManager();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to