From 307dd00efb18c681dee2c0215251aae2a392c2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Zmys=C5=82owski?= Date: Thu, 6 Oct 2011 08:10:51 +0200 Subject: [PATCH] Fixed #3072 (improve check: deprecated function alloca) --- cli/cmdlineparser.cpp | 7 +++++++ lib/checkobsoletefunctions.cpp | 26 +++++++++++++++++++------- lib/checkobsoletefunctions.h | 2 ++ lib/settings.h | 3 +++ test/testcmdlineparser.cpp | 11 +++++++++++ test/testobsoletefunctions.cpp | 12 ++++++++++++ 6 files changed, 54 insertions(+), 7 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 941c7dd9b..22837da9f 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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= Suppress warnings that match . The format of\n" " is:\n" " [error id]:[filename]:[line]\n" diff --git a/lib/checkobsoletefunctions.cpp b/lib/checkobsoletefunctions.cpp index 94e4aeee1..4897e5ac0 100644 --- a/lib/checkobsoletefunctions.cpp +++ b/lib/checkobsoletefunctions.cpp @@ -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; + } } } } diff --git a/lib/checkobsoletefunctions.h b/lib/checkobsoletefunctions.h index 776478f4e..d5b46bec4 100644 --- a/lib/checkobsoletefunctions.h +++ b/lib/checkobsoletefunctions.h @@ -63,6 +63,7 @@ private: /* function name / error message */ std::map _obsoleteStandardFunctions; std::map _obsoletePosixFunctions; + std::map _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)."; } diff --git a/lib/settings.h b/lib/settings.h index c41c10160..0af04fe6a 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -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; diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 3a603c09a..73ab69919 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -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! diff --git a/test/testobsoletefunctions.cpp b/test/testobsoletefunctions.cpp index 12cbf27ce..562230b64 100644 --- a/test/testobsoletefunctions.cpp +++ b/test/testobsoletefunctions.cpp @@ -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() {