#6181 Drop redundantGetAndSetUserId. Ensure (style-) warnings are issued if setuid()/getuid() and some related functions are ignored. Move strdupa()/strndupa() from posix.cfg to gnu.cfg

This commit is contained in:
Alexander Mai 2015-02-16 22:19:51 +01:00
parent 115cefc8fb
commit 0a6babea74
9 changed files with 84 additions and 89 deletions

View File

@ -4,4 +4,23 @@
<dealloc>free</dealloc>
<alloc init="true">get_current_dir_name</alloc>
</memory>
<function name="strndupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
<arg nr="2">
<not-uninit/>
</arg>
</function>
<function name="strdupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
</function>
</def>

View File

@ -67,14 +67,6 @@
<not-uninit/>
</arg>
</function>
<function name="strdupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
</function>
<function name="strndup">
<noreturn>false</noreturn>
<arg nr="1">
@ -85,17 +77,6 @@
<not-uninit/>
</arg>
</function>
<function name="strndupa">
<use-retval/>
<noreturn>false</noreturn>
<arg nr="1">
<not-null/>
<not-uninit/>
</arg>
<arg nr="2">
<not-uninit/>
</arg>
</function>
<function name="wcsdup">
<noreturn>false</noreturn>
<arg nr="1">
@ -634,6 +615,50 @@
<not-uninit/>
</arg>
</function>
<function name="getgid">
<noreturn>false</noreturn>
<use-retval/>
</function>
<function name="getegid">
<noreturn>false</noreturn>
<use-retval/>
</function>
<function name="getuid">
<noreturn>false</noreturn>
<use-retval/>
</function>
<function name="geteuid">
<noreturn>false</noreturn>
<use-retval/>
</function>
<function name="setuid">
<noreturn>false</noreturn>
<use-retval/>
<arg nr="1">
<not-uninit/>
</arg>
</function>
<function name="seteuid">
<noreturn>false</noreturn>
<use-retval/>
<arg nr="1">
<not-uninit/>
</arg>
</function>
<function name="setgid">
<noreturn>false</noreturn>
<use-retval/>
<arg nr="1">
<not-uninit/>
</arg>
</function>
<function name="setegid">
<noreturn>false</noreturn>
<use-retval/>
<arg nr="1">
<not-uninit/>
</arg>
</function>
<memory>
<dealloc>free</dealloc>
<alloc init="true">strdup</alloc>

View File

@ -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.
//---------------------------------------------------------------------------

View File

@ -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();

View File

@ -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 <string.h>
#include <unistd.h>
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);
}

View File

@ -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();
}

View File

@ -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

View File

@ -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
//

View File

@ -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"