BUG: fix memory leak and cleanup error string code

This commit is contained in:
Ken Martin 2008-06-28 11:16:36 -04:00
parent 3cc9efceb7
commit 52d8b1b5d9
3 changed files with 25 additions and 35 deletions

View File

@ -76,15 +76,15 @@ IsFunctionBlocked(const cmListFileFunction& lff,
} }
else else
{ {
char* errorString = 0; std::string errorString;
std::vector<std::string> expandedArguments; std::vector<std::string> expandedArguments;
mf.ExpandArguments(this->Functions[c].Arguments, mf.ExpandArguments(this->Functions[c].Arguments,
expandedArguments); expandedArguments);
bool isTrue = bool isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,&mf); cmIfCommand::IsTrue(expandedArguments,errorString,&mf);
if (errorString) if (errorString.size())
{ {
std::string err = "had incorrect arguments: "; std::string err = "had incorrect arguments: ";
unsigned int i; unsigned int i;
@ -99,7 +99,6 @@ IsFunctionBlocked(const cmListFileFunction& lff,
err += errorString; err += errorString;
err += ")."; err += ").";
cmSystemTools::Error(err.c_str()); cmSystemTools::Error(err.c_str());
delete [] errorString;
return false; return false;
} }
@ -183,14 +182,14 @@ bool cmIfCommand
::InvokeInitialPass(const std::vector<cmListFileArgument>& args, ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
cmExecutionStatus &) cmExecutionStatus &)
{ {
char* errorString = 0; std::string errorString;
std::vector<std::string> expandedArguments; std::vector<std::string> expandedArguments;
this->Makefile->ExpandArguments(args, expandedArguments); this->Makefile->ExpandArguments(args, expandedArguments);
bool isTrue = bool isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,this->Makefile); cmIfCommand::IsTrue(expandedArguments,errorString,this->Makefile);
if (errorString) if (errorString.size())
{ {
std::string err = "had incorrect arguments: "; std::string err = "had incorrect arguments: ";
unsigned int i; unsigned int i;
@ -205,7 +204,6 @@ bool cmIfCommand
err += errorString; err += errorString;
err += ")."; err += ").";
this->SetError(err.c_str()); this->SetError(err.c_str());
delete [] errorString;
return false; return false;
} }
@ -290,7 +288,7 @@ namespace
// level 0 processes parenthetical expressions // level 0 processes parenthetical expressions
bool HandleLevel0(std::list<std::string> &newArgs, bool HandleLevel0(std::list<std::string> &newArgs,
cmMakefile *makefile, cmMakefile *makefile,
char **errorString) std::string &errorString)
{ {
int reducible; int reducible;
do do
@ -320,11 +318,7 @@ namespace
} }
if (depth) if (depth)
{ {
cmOStringStream error; errorString = "mismatched parenthesis in condition";
error << "mismatched parenthesis in condition";
delete [] *errorString;
*errorString = new char[error.str().size() + 1];
strcpy(*errorString, error.str().c_str());
return false; return false;
} }
// store the reduced args in this vector // store the reduced args in this vector
@ -365,7 +359,7 @@ namespace
// level one handles most predicates except for NOT // level one handles most predicates except for NOT
bool HandleLevel1(std::list<std::string> &newArgs, bool HandleLevel1(std::list<std::string> &newArgs,
cmMakefile *makefile, cmMakefile *makefile,
char **) std::string &)
{ {
int reducible; int reducible;
do do
@ -442,7 +436,7 @@ namespace
// level two handles most binary operations except for AND OR // level two handles most binary operations except for AND OR
bool HandleLevel2(std::list<std::string> &newArgs, bool HandleLevel2(std::list<std::string> &newArgs,
cmMakefile *makefile, cmMakefile *makefile,
char **errorString) std::string &errorString)
{ {
int reducible; int reducible;
const char *def; const char *def;
@ -468,9 +462,7 @@ namespace
{ {
cmOStringStream error; cmOStringStream error;
error << "Regular expression \"" << rex << "\" cannot compile"; error << "Regular expression \"" << rex << "\" cannot compile";
delete [] *errorString; errorString = error.str();
*errorString = new char[error.str().size() + 1];
strcpy(*errorString, error.str().c_str());
return false; return false;
} }
if (regEntry.find(def)) if (regEntry.find(def))
@ -577,7 +569,7 @@ namespace
// level 3 handles NOT // level 3 handles NOT
bool HandleLevel3(std::list<std::string> &newArgs, bool HandleLevel3(std::list<std::string> &newArgs,
cmMakefile *makefile, cmMakefile *makefile,
char **) std::string &)
{ {
int reducible; int reducible;
const char *def; const char *def;
@ -608,7 +600,7 @@ namespace
// level 4 handles AND OR // level 4 handles AND OR
bool HandleLevel4(std::list<std::string> &newArgs, bool HandleLevel4(std::list<std::string> &newArgs,
cmMakefile *makefile, cmMakefile *makefile,
char **) std::string &)
{ {
int reducible; int reducible;
const char *def; const char *def;
@ -669,19 +661,14 @@ namespace
bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
char **errorString, cmMakefile *makefile) std::string &errorString, cmMakefile *makefile)
{ {
// check for the different signatures
const char *def; const char *def;
const char* msg = "Unknown arguments specified"; errorString = "";
*errorString = new char[strlen(msg) + 1];
strcpy(*errorString, msg);
// handle empty invocation // handle empty invocation
if (args.size() < 1) if (args.size() < 1)
{ {
delete [] *errorString;
*errorString = 0;
return false; return false;
} }
@ -720,8 +707,6 @@ bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
// now at the end there should only be one argument left // now at the end there should only be one argument left
if (newArgs.size() == 1) if (newArgs.size() == 1)
{ {
delete [] *errorString;
*errorString = 0;
if (*newArgs.begin() == "0") if (*newArgs.begin() == "0")
{ {
return false; return false;
@ -736,6 +721,11 @@ bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
return false; return false;
} }
} }
else
{
errorString = "Unknown arguments specified";
return false;
}
return true; return true;
} }

View File

@ -184,7 +184,7 @@ public:
// arguments were valid, and if so, was the response true. If there is // arguments were valid, and if so, was the response true. If there is
// an error, the errorString will be set. // an error, the errorString will be set.
static bool IsTrue(const std::vector<std::string> &args, static bool IsTrue(const std::vector<std::string> &args,
char** errorString, cmMakefile *mf); std::string &errorString, cmMakefile *mf);
// Get a definition from the makefile. If it doesn't exist, // Get a definition from the makefile. If it doesn't exist,
// return the original string. // return the original string.

View File

@ -39,12 +39,12 @@ IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
// if this is the endwhile for this while loop then execute // if this is the endwhile for this while loop then execute
if (!this->Depth) if (!this->Depth)
{ {
char* errorString = 0; std::string errorString;
std::vector<std::string> expandedArguments; std::vector<std::string> expandedArguments;
mf.ExpandArguments(this->Args, expandedArguments); mf.ExpandArguments(this->Args, expandedArguments);
bool isTrue = bool isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,&mf); cmIfCommand::IsTrue(expandedArguments,errorString,&mf);
this->Executing = true; this->Executing = true;
while (isTrue) while (isTrue)
@ -69,7 +69,7 @@ IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
expandedArguments.clear(); expandedArguments.clear();
mf.ExpandArguments(this->Args, expandedArguments); mf.ExpandArguments(this->Args, expandedArguments);
isTrue = isTrue =
cmIfCommand::IsTrue(expandedArguments,&errorString,&mf); cmIfCommand::IsTrue(expandedArguments,errorString,&mf);
} }
mf.RemoveFunctionBlocker(lff); mf.RemoveFunctionBlocker(lff);
return true; return true;