Fixed #3072 (improve check: deprecated function alloca)

This commit is contained in:
Marek Zmysłowski 2011-10-06 08:10:51 +02:00 committed by Daniel Marjamäki
parent 09109f19f8
commit 307dd00efb
6 changed files with 54 additions and 7 deletions

View File

@ -431,6 +431,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
_settings->posix = true;
}
// --C99
else if (strcmp(argv[i], "--std=c99") == 0)
{
_settings->c99 = true;
}
// Output formatter
else if (strcmp(argv[i], "--template") == 0)
{
@ -767,6 +773,7 @@ void CmdLineParser::PrintHelp()
#endif
" -s, --style Deprecated, use --enable=style\n"
" --std=posix Code is posix\n"
" --std=c99 Code is C99 standard\n"
" --suppress=<spec> Suppress warnings that match <spec>. The format of\n"
" <spec> is:\n"
" [error id]:[filename]:[line]\n"

View File

@ -63,15 +63,27 @@ void CheckObsoleteFunctions::obsoleteFunctions()
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
break;
}
else if (_settings->posix)
else
{
it = _obsoletePosixFunctions.find(tok->str());
if (it != _obsoletePosixFunctions.end())
if (_settings->posix)
{
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
break;
it = _obsoletePosixFunctions.find(tok->str());
if (it != _obsoletePosixFunctions.end())
{
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
break;
}
}
if (_settings->c99)
{
it = _obsoleteC99Functions.find(tok->str());
if (it != _obsoleteC99Functions.end())
{
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
break;
}
}
}
}

View File

@ -63,6 +63,7 @@ private:
/* function name / error message */
std::map<std::string, std::string> _obsoleteStandardFunctions;
std::map<std::string, std::string> _obsoletePosixFunctions;
std::map<std::string, std::string> _obsoleteC99Functions;
/** init obsolete functions list ' */
void initObsoleteFunctions()
@ -111,6 +112,7 @@ private:
_obsoleteStandardFunctions["gets"] = "Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n"
"Found obsolete function 'gets'. With gets you'll get buffer overruns if the input data too big for the buffer. It is recommended to use the function 'fgets' instead.";
_obsoleteC99Functions["alloca"] = "Found obsolete function 'alloca'. It is recommended to use a variable length array.\nFound obsolete function 'alloca'. It is recommended to use a variable length array or a dynamically allocated array. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).";
}

View File

@ -191,6 +191,9 @@ public:
/** Code is posix - it is not compatible with non-posix environments */
bool posix;
/** Code is C99 standard - it is not compatible with previous versions */
bool c99;
/** size of standard types */
unsigned int sizeof_bool;
unsigned int sizeof_short;

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(jobsInvalid);
TEST_CASE(reportProgressTest); // "Test" suffix to avoid hiding the parent's reportProgress
TEST_CASE(stdposix);
TEST_CASE(stdc99);
TEST_CASE(suppressionsOld); // TODO: Create and test real suppression file
TEST_CASE(suppressions);
TEST_CASE(suppressionsNoFile);
@ -602,6 +603,16 @@ private:
ASSERT(settings.posix);
}
void stdc99()
{
REDIRECT;
const char *argv[] = {"cppcheck", "--std=c99", "file.cpp"};
Settings settings;
CmdLineParser parser(&settings);
ASSERT(parser.ParseFromArgs(3, argv));
ASSERT(settings.c99);
}
void suppressionsOld()
{
// TODO: Fails because there is no suppr.txt file!

View File

@ -49,6 +49,8 @@ private:
// dangerous function
TEST_CASE(testgets);
TEST_CASE(testalloca);
// declared function ticket #3121
TEST_CASE(test_declared_function);
}
@ -61,6 +63,7 @@ private:
Settings settings;
settings.addEnabled("style");
settings.posix = true;
settings.c99 = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -216,6 +219,15 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
}
void testalloca()
{
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'alloca'. It is recommended to use a variable length array.\n", errout.str());
}
// ticket #3121
void test_declared_function()
{