Fix crash when giving -D without argument.

Fixes ticket #2476 (Crash when -D used without parameters.)
This commit is contained in:
Kimmo Varis 2011-01-17 21:03:22 +02:00
parent b5020468f6
commit 6dd16d3919
2 changed files with 30 additions and 4 deletions

View File

@ -225,12 +225,28 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
// User define
else if (strncmp(argv[i], "-D", 2) == 0)
{
std::string define;
// "-D define"
if (strcmp(argv[i], "-D") == 0)
{
++i;
if (i >= argc)
{
PrintMessage("cppcheck: argument to '-D' is missing");
return false;
}
define = argv[i];
}
// "-Ddefine"
else
{
define = 2 + argv[i];
}
if (!_settings->userDefines.empty())
_settings->userDefines += ";";
if (strcmp(argv[i], "-D") == 0)
_settings->userDefines += argv[++i];
else
_settings->userDefines += 2 + argv[i];
_settings->userDefines += define;
}
// Include paths

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(forcelong);
TEST_CASE(quietshort);
TEST_CASE(quietlong);
TEST_CASE(defines_noarg);
TEST_CASE(defines);
TEST_CASE(defines2);
TEST_CASE(defines3);
@ -230,6 +231,15 @@ private:
ASSERT_EQUALS(true, settings._errorsOnly);
}
void defines_noarg()
{
REDIRECT;
const char *argv[] = {"cppcheck", "-D"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT_EQUALS(false, parser.ParseFromArgs(2, argv));
}
void defines()
{
REDIRECT;