diff options
author | George Burgess IV <gbiv@google.com> | 2018-02-17 21:37:11 -0800 |
---|---|---|
committer | George Burgess <gbiv@google.com> | 2018-02-20 19:50:29 +0000 |
commit | 2c2eccb3796179664ccda7e1f9b981aad0c2e3ef (patch) | |
tree | 162f5ce85863c086f8a1a66cf4e7f1fc50fdc4be | |
parent | 1b6fd73cd008aa3e2715fbc6f1b2a7dd05c92e04 (diff) | |
download | slang-2c2eccb3796179664ccda7e1f9b981aad0c2e3ef.tar.gz |
Fix a memory leak
Analyzer complaint:
frameworks/compile/slang/slang.cpp:336:12: warning: Potential leak of
memory pointed to by 'OS'
This is a bit tricky: llvm::tool_output_file's ctor might write a
failure value to EC. If so, we'd take the `if (EC) return false;`
branch in Slang::setOutput, and fail to free `OS`.
Using a unique_ptr instead fixes all of our problems.
This also removes a nullptr check; `new` without `std::nothrow` can't
return null, so the null check was unnecessary.
Bug: None
Test: Ran the static analyzer. No complaints about leaky memory.
Change-Id: I22c865ea4ef8caf1d8c3b14939a6d28850f587c4
-rw-r--r-- | slang.cpp | 14 |
1 files changed, 5 insertions, 9 deletions
@@ -128,7 +128,7 @@ static llvm::LLVMContext globalContext; llvm::LLVMContext &getGlobalLLVMContext() { return globalContext; } -static inline llvm::tool_output_file * +static inline std::unique_ptr<llvm::tool_output_file> OpenOutputFile(const char *OutputFile, llvm::sys::fs::OpenFlags Flags, std::error_code &EC, @@ -139,10 +139,7 @@ OpenOutputFile(const char *OutputFile, EC = llvm::sys::fs::create_directories( llvm::sys::path::parent_path(OutputFile)); if (!EC) { - llvm::tool_output_file *F = - new llvm::tool_output_file(OutputFile, EC, Flags); - if (F != nullptr) - return F; + return llvm::make_unique<llvm::tool_output_file>(OutputFile, EC, Flags); } // Report error here. @@ -310,7 +307,7 @@ bool Slang::setInputSource(llvm::StringRef InputFile) { bool Slang::setOutput(const char *OutputFile) { std::error_code EC; - llvm::tool_output_file *OS = nullptr; + std::unique_ptr<llvm::tool_output_file> OS; switch (mOT) { case OT_Dependency: @@ -335,7 +332,7 @@ bool Slang::setOutput(const char *OutputFile) { if (EC) return false; - mOS.reset(OS); + mOS = std::move(OS); mOutputFileName = OutputFile; @@ -345,8 +342,7 @@ bool Slang::setOutput(const char *OutputFile) { bool Slang::setDepOutput(const char *OutputFile) { std::error_code EC; - mDOS.reset( - OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine)); + mDOS = OpenOutputFile(OutputFile, llvm::sys::fs::F_Text, EC, mDiagEngine); if (EC || (mDOS.get() == nullptr)) return false; |