summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlp Toker <alp@nuanti.com>2014-05-31 00:02:21 +0000
committerAlp Toker <alp@nuanti.com>2014-05-31 00:02:21 +0000
commitffea7e328975034b52a7ce3490d36d22b30d9cc8 (patch)
tree54a962d3fb95751461f8e313b4e080549c632c6c /tools
parente94666444591bf82a39aa486471c63bf82b1793b (diff)
downloadclang-ffea7e328975034b52a7ce3490d36d22b30d9cc8.tar.gz
clang-ffea7e328975034b52a7ce3490d36d22b30d9cc8.tar.bz2
clang-ffea7e328975034b52a7ce3490d36d22b30d9cc8.tar.xz
cc1as: fix a potential leak and unremoved output file in error conditions
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209935 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/driver/cc1as_main.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/tools/driver/cc1as_main.cpp b/tools/driver/cc1as_main.cpp
index c4456e2f1d..11565e57b5 100644
--- a/tools/driver/cc1as_main.cpp
+++ b/tools/driver/cc1as_main.cpp
@@ -266,6 +266,7 @@ static formatted_raw_ostream *GetOutputStream(AssemblerInvocation &Opts,
if (!Error.empty()) {
Diags.Report(diag::err_fe_unable_to_open_output)
<< Opts.OutputPath << Error;
+ delete Out;
return 0;
}
@@ -276,24 +277,23 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags) {
// Get the target specific parser.
std::string Error;
- const Target *TheTarget(TargetRegistry::lookupTarget(Opts.Triple, Error));
+ const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
if (!TheTarget) {
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
return false;
}
- std::unique_ptr<MemoryBuffer> BufferPtr;
- if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, BufferPtr)) {
+ std::unique_ptr<MemoryBuffer> Buffer;
+ if (error_code ec = MemoryBuffer::getFileOrSTDIN(Opts.InputFile, Buffer)) {
Error = ec.message();
Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
return false;
}
- MemoryBuffer *Buffer = BufferPtr.release();
SourceMgr SrcMgr;
// Tell SrcMgr about this buffer, which is what the parser will pick up.
- SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+ SrcMgr.AddNewSourceBuffer(Buffer.release(), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
@@ -311,7 +311,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
MAI->setCompressDebugSections(true);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
- formatted_raw_ostream *Out = GetOutputStream(Opts, Diags, IsBinary);
+ std::unique_ptr<formatted_raw_ostream> Out(
+ GetOutputStream(Opts, Diags, IsBinary));
if (!Out)
return false;
@@ -380,6 +381,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
Str.get()->InitSections();
}
+ bool Success = true;
+
std::unique_ptr<MCAsmParser> Parser(
createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
@@ -389,17 +392,18 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts,
TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
if (!TAP) {
Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
- return false;
+ Success = false;
}
- Parser->setTargetParser(*TAP.get());
-
- bool Success = !Parser->Run(Opts.NoInitialTextSection);
+ if (Success) {
+ Parser->setTargetParser(*TAP.get());
+ Success = !Parser->Run(Opts.NoInitialTextSection);
+ }
- // Close the output.
- delete Out;
+ // Close the output stream early.
+ Out.reset();
- // Delete output on errors.
+ // Delete output file if there were errors.
if (!Success && Opts.OutputPath != "-")
sys::fs::remove(Opts.OutputPath);