From 1e3c0a4c77611f99f1e482de256c59358a902b63 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 24 Jul 2013 14:32:01 +0000 Subject: [PATCH] Don't leak when expanding response files. Before this patch we would strdup each argument. If one was a response file, we would replace it with the response file contents, leaking the original strdup result. We now don't strdup the originals and let StringSaver free any memory it allocated. This also saves a bit of malloc traffic when response files are not used. Leak found by the valgrind build bot. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187042 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/CommandLine.cpp | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index c2a25a18cd9..1975a013385 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -563,8 +563,19 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, namespace { class StrDupSaver : public StringSaver { + std::vector Dups; + public: + ~StrDupSaver() { + for (std::vector::iterator I = Dups.begin(), E = Dups.end(); + I != E; ++I) { + char *Dup = *I; + free(Dup); + } + } const char *SaveString(const char *Str) LLVM_OVERRIDE { - return strdup(Str); + char *Dup = strdup(Str); + Dups.push_back(Dup); + return Dup; } }; } @@ -588,20 +599,14 @@ void cl::ParseEnvironmentOptions(const char *progName, const char *envVar, // Get program's "name", which we wouldn't know without the caller // telling us. SmallVector newArgv; - newArgv.push_back(strdup(progName)); + StrDupSaver Saver; + newArgv.push_back(Saver.SaveString(progName)); // Parse the value of the environment variable into a "command line" // and hand it off to ParseCommandLineOptions(). - StrDupSaver Saver; TokenizeGNUCommandLine(envValue, Saver, newArgv); int newArgc = static_cast(newArgv.size()); ParseCommandLineOptions(newArgc, &newArgv[0], Overview); - - // Free all the strdup()ed strings. - for (SmallVectorImpl::iterator i = newArgv.begin(), - e = newArgv.end(); - i != e; ++i) - free(const_cast(*i)); } void cl::ParseCommandLineOptions(int argc, const char * const *argv, @@ -618,7 +623,7 @@ void cl::ParseCommandLineOptions(int argc, const char * const *argv, // Expand response files. SmallVector newArgv; for (int i = 0; i != argc; ++i) - newArgv.push_back(strdup(argv[i])); + newArgv.push_back(argv[i]); StrDupSaver Saver; ExpandResponseFiles(Saver, TokenizeGNUCommandLine, newArgv); argv = &newArgv[0]; @@ -914,13 +919,6 @@ void cl::ParseCommandLineOptions(int argc, const char * const *argv, PositionalOpts.clear(); MoreHelp->clear(); - // Free the memory allocated by ExpandResponseFiles. - // Free all the strdup()ed strings. - for (SmallVectorImpl::iterator i = newArgv.begin(), - e = newArgv.end(); - i != e; ++i) - free(const_cast(*i)); - // If we had an error processing our arguments, don't let the program execute if (ErrorParsing) exit(1); } -- 2.34.1