diff --git a/cfg/gnu.cfg b/cfg/gnu.cfg index 04dc7570b..7e6184295 100644 --- a/cfg/gnu.cfg +++ b/cfg/gnu.cfg @@ -4,4 +4,23 @@ free get_current_dir_name + + + false + + + + + + + + + + + false + + + + + diff --git a/cfg/posix.cfg b/cfg/posix.cfg index f0f416fa4..53783c08e 100644 --- a/cfg/posix.cfg +++ b/cfg/posix.cfg @@ -67,14 +67,6 @@ - - - false - - - - - false @@ -85,17 +77,6 @@ - - - false - - - - - - - - false @@ -634,6 +615,50 @@ + + false + + + + false + + + + false + + + + false + + + + false + + + + + + + false + + + + + + + false + + + + + + + false + + + + + free strdup diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6922612c4..14615f652 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2320,42 +2320,6 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* to "for both parameters leads to a statement which is always " + strResult + "."); } -//----------------------------------------------------------------------------- -// Check for code like: -// seteuid(geteuid()) or setuid(getuid()), which first gets and then sets the -// (effective) user id to itself. Very often this indicates a copy and paste -// error. -//----------------------------------------------------------------------------- -void CheckOther::redundantGetAndSetUserId() -{ - if (!_settings->standards.posix || !_settings->isEnabled("warning")) - return; - - const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); - - const std::size_t functions = symbolDatabase->functionScopes.size(); - for (std::size_t i = 0; i < functions; ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; - // check all the code in the function - for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { - if (Token::simpleMatch(tok, "setuid ( getuid ( ) )") - || Token::simpleMatch(tok, "seteuid ( geteuid ( ) )") - || Token::simpleMatch(tok, "setgid ( getgid ( ) )") - || Token::simpleMatch(tok, "setegid ( getegid ( ) )")) { - redundantGetAndSetUserIdError(tok); - } - } - } -} -void CheckOther::redundantGetAndSetUserIdError(const Token *tok) -{ - reportError(tok, Severity::warning, - "redundantGetAndSetUserId", "Redundant get and set of user id.\n" - "Redundant statement without any effect. First the user id is retrieved" - "by get(e)uid() and then set with set(e)uid().", false); -} - - //--------------------------------------------------------------------------- // Check testing sign of unsigned variables and pointers. //--------------------------------------------------------------------------- diff --git a/lib/checkother.h b/lib/checkother.h index a8527ec2d..b398f492e 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -95,7 +95,6 @@ public: checkOther.checkZeroDivision(); checkOther.checkMathFunctions(); - checkOther.redundantGetAndSetUserId(); checkOther.checkMisusedScopedObject(); checkOther.checkMemsetZeroBytes(); checkOther.checkMemsetInvalid2ndParam(); @@ -159,9 +158,6 @@ public: /** @brief %Check for parameters given to math function that do not make sense*/ void checkMathFunctions(); - /** @brief % Check for seteuid(geteuid()) or setuid(getuid())*/ - void redundantGetAndSetUserId(); - /** @brief copying to memory or assigning to a variable twice */ void checkRedundantAssignment(); diff --git a/test/cfg/gnu.c b/test/cfg/gnu.c index f21711dbb..883f4180a 100644 --- a/test/cfg/gnu.c +++ b/test/cfg/gnu.c @@ -2,21 +2,20 @@ // Test library configuration for gnu.cfg // // Usage: -// $ cppcheck --check-library --library=gnu --enable=information --enable=style --error-exitcode=1 --inline-suppr test/cfg/gnu.c +// $ cppcheck --check-library --library=gnu --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr test/cfg/gnu.c // => // No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0 // #include -#include void leakReturnValNotUsed() { // cppcheck-suppress unreadVariable - char* ptr = strdupa("test"); + char* ptr = (char*)strdupa("test"); // cppcheck-suppress ignoredReturnValue strdupa("test"); // cppcheck-suppress unreadVariable - char* ptr2 = strndupa("test", 1); + char* ptr2 = (char*)strndupa("test", 1); // cppcheck-suppress ignoredReturnValue strndupa("test", 1); } diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 0a6795725..9aaf42881 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -2,7 +2,7 @@ // Test library configuration for posix.cfg // // Usage: -// $ cppcheck --check-library --library=posix --enable=information --error-exitcode=1 --inline-suppr cfg/test/posix.c +// $ cppcheck --check-library --library=posix --enable=information --error-exitcode=1 --inline-suppr --suppress=missingIncludeSystem test/cfg/posix.c // => // No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0 // @@ -35,6 +35,7 @@ void bufferAccessOutOfBounds(int fd) { sendto(fd,a,5,0,0x0,0x0); // cppcheck-suppress bufferAccessOutOfBounds sendto(fd,a,6,0,0x0,0x0); + // cppcheck-suppress constStatement 0; } @@ -46,7 +47,8 @@ void nullPointer(char *p) { readdir (0); } -void memleak_mmap(int fd) { +void memleak_mmap(int fd) { + // cppcheck-suppress unreadVariable void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0); // cppcheck-suppress memleak } @@ -59,16 +61,19 @@ void resourceLeak_fdopen(int fd) { */ void resourceLeak_fdopendir(int fd) { + // cppcheck-suppress unreadVariable DIR* leak1 = fdopendir(fd); // cppcheck-suppress resourceLeak } void resourceLeak_opendir(void) { + // cppcheck-suppress unreadVariable DIR* leak1 = opendir("abc"); // cppcheck-suppress resourceLeak } void resourceLeak_socket(void) { + // cppcheck-suppress unreadVariable int s = socket(AF_INET, SOCK_STREAM, 0); // cppcheck-suppress resourceLeak } @@ -89,10 +94,13 @@ void noleak(int x, int y, int z) { // unused return value void ignoredReturnValue(void *addr, int fd) { + // cppcheck-suppress ignoredReturnValue // cppcheck-suppress leakReturnValNotUsed mmap(addr, 255, PROT_NONE, MAP_PRIVATE, fd, 0); // cppcheck-suppress ignoredReturnValue - strdupa("ab"); + setuid(42); + // cppcheck-suppress ignoredReturnValue + getuid(); } diff --git a/test/cfg/runtests.sh b/test/cfg/runtests.sh index 2b64195ff..77a41160a 100755 --- a/test/cfg/runtests.sh +++ b/test/cfg/runtests.sh @@ -11,8 +11,12 @@ fi # posix.c gcc -fsyntax-only ${DIR}posix.c -${CPPCHECK} --check-library --library=posix --enable=information --error-exitcode=1 --inline-suppr ${DIR}posix.c +${CPPCHECK} --check-library --library=posix --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr ${DIR}posix.c + +# gnu.c +gcc -fsyntax-only -D_GNU_SOURCE ${DIR}gnu.c +${CPPCHECK} --check-library --library=gnu --enable=information --enable=style --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr ${DIR}gnu.c # std.c gcc -fsyntax-only ${DIR}std.c -${CPPCHECK} --check-library --enable=information --error-exitcode=1 --inline-suppr ${DIR}std.c +${CPPCHECK} --check-library --enable=information --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr ${DIR}std.c diff --git a/test/cfg/std.c b/test/cfg/std.c index c58e9c979..dd44f26f4 100644 --- a/test/cfg/std.c +++ b/test/cfg/std.c @@ -2,7 +2,7 @@ // Test library configuration for std.cfg // // Usage: -// $ cppcheck --check-library --enable=information --error-exitcode=1 --inline-suppr cfg/test/std.c +// $ cppcheck --check-library --enable=information --error-exitcode=1 --suppress=missingIncludeSystem --inline-suppr test/cfg/std.c // => // No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0 // diff --git a/test/testother.cpp b/test/testother.cpp index 47055bb52..aae7e4457 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -124,8 +124,6 @@ private: TEST_CASE(memsetZeroBytes); TEST_CASE(memsetInvalid2ndParam); - TEST_CASE(redundantGetAndSetUserId); - TEST_CASE(clarifyCalculation); TEST_CASE(clarifyStatement); @@ -3732,24 +3730,6 @@ private: ASSERT_EQUALS("[test.cpp:4]: (portability) The 2nd memset() argument '1.0f+i' is a float, its representation is implementation defined.\n", errout.str()); } - void redundantGetAndSetUserId() { - checkposix("void foo() { seteuid(geteuid()); }"); - ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str()); - checkposix("void foo() { setuid(getuid()); }"); - ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str()); - checkposix("void foo() { setgid(getgid()); }"); - ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str()); - checkposix("void foo() { setegid(getegid()); }"); - ASSERT_EQUALS("[test.cpp:1]: (warning) Redundant get and set of user id.\n", errout.str()); - - check("void foo() { seteuid(getuid()); }"); - ASSERT_EQUALS("", errout.str()); - check("void foo() { seteuid(foo()); }"); - ASSERT_EQUALS("", errout.str()); - check("void foo() { foo(getuid()); }"); - ASSERT_EQUALS("", errout.str()); - } - void clarifyCalculation() { check("int f(char c) {\n" " return 10 * (c == 0) ? 1 : 2;\n"