Refactoring: Replaced CheckOther::checkCCTypeFunctions with configuration

This commit is contained in:
Daniel Marjamäki 2013-12-23 19:58:33 +01:00
parent 8ad33273c8
commit ac30a84c3a
4 changed files with 11 additions and 172 deletions

View File

@ -18,12 +18,17 @@
<function name="fclose"> <noreturn>false</noreturn> </function>
<function name="fflush"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isalha"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isalnum"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isdigit"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isxdigit"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="islower"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isupper"> <noreturn>false</noreturn> <leak-ignore/> </function>
<function name="isalha"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isalnum"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="iscntrl"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isdigit"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isxdigit"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isgraph"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="islower"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isupper"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isprint"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="ispunct"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="isspace"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="1"><valid>0-255</valid></arg> </function>
<function name="memchr"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="3"><not-bool/><valid>0-</valid></arg> </function>
<function name="memcmp"> <noreturn>false</noreturn> <leak-ignore/> <arg nr="3"><not-bool/><valid>0-</valid></arg> </function>

View File

@ -2413,24 +2413,6 @@ void CheckOther::mathfunctionCallError(const Token *tok, const unsigned int numP
} else
reportError(tok, Severity::error, "wrongmathcall", "Passing value '#' to #() leads to undefined result.");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void CheckOther::checkCCTypeFunctions()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->varId() == 0 &&
Token::Match(tok, "isalnum|isalpha|iscntrl|isdigit|isgraph|islower|isprint|ispunct|isspace|isupper|isxdigit ( %num% ,|)") &&
MathLib::isNegative(tok->strAt(2))) {
cctypefunctionCallError(tok, tok->str(), tok->strAt(2));
}
}
}
void CheckOther::cctypefunctionCallError(const Token *tok, const std::string &functionName, const std::string &value)
{
reportError(tok, Severity::error, "wrongcctypecall", "Passing value " + value + " to " + functionName + "() causes undefined behavior which may lead to a crash.");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void CheckOther::checkMisusedScopedObject()

View File

@ -92,7 +92,6 @@ public:
checkOther.checkZeroDivision();
checkOther.checkZeroDivisionOrUselessCondition();
checkOther.checkMathFunctions();
checkOther.checkCCTypeFunctions();
checkOther.redundantGetAndSetUserId();
checkOther.checkIncorrectLogicOperator();
@ -174,9 +173,6 @@ public:
/** @brief %Check for parameters given to math function that do not make sense*/
void checkMathFunctions();
/** @brief %Check for parameters given to cctype function that do make error*/
void checkCCTypeFunctions();
/** @brief % Check for seteuid(geteuid()) or setuid(getuid())*/
void redundantGetAndSetUserId();
@ -295,7 +291,6 @@ private:
void zerodivcondError(const Token *tokcond, const Token *tokdiv);
void nanInArithmeticExpressionError(const Token *tok);
void mathfunctionCallError(const Token *tok, const unsigned int numParam = 1);
void cctypefunctionCallError(const Token *tok, const std::string &functionName, const std::string &value);
void redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
void redundantAssignmentInSwitchError(const Token *tok1, const Token *tok2, const std::string &var);
void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var);
@ -392,7 +387,6 @@ private:
c.pointerLessThanZeroError(0, false);
c.pointerPositiveError(0, false);
c.SuspiciousSemicolonError(0);
c.cctypefunctionCallError(0, "funname", "value");
c.moduloAlwaysTrueFalseError(0, "1");
c.incompleteArrayFillError(0, "buffer", "memset", false);
c.varFuncNullUBError(0);
@ -458,7 +452,6 @@ private:
"* testing if unsigned variable is negative\n"
"* testing is unsigned variable is positive\n"
"* Suspicious use of ; at the end of 'if/for/while' statement.\n"
"* incorrect usage of functions from ctype library.\n"
"* Comparisons of modulo results that are always true/false.\n"
"* Array filled incompletely using memset/memcpy/memmove.\n"
"* redundant get and set function of user id (--std=posix).\n"

View File

@ -93,7 +93,6 @@ private:
TEST_CASE(mathfunctionCall_asin);
TEST_CASE(mathfunctionCall_pow);
TEST_CASE(mathfunctionCall_atan2);
TEST_CASE(cctypefunctionCall);
TEST_CASE(switchRedundantAssignmentTest);
TEST_CASE(switchRedundantOperationTest);
@ -1778,146 +1777,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void cctypefunctionCall() {
// isalnum
check("void foo()\n"
"{\n"
" std::cout << isalnum(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isalnum(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isalnum() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isalpha(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isalpha(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isalpha() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << iscntrl(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << iscntrl(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to iscntrl() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isdigit(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isdigit(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isdigit() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isgraph(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isgraph(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isgraph() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << islower(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << islower(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to islower() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isprint(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isprint(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isprint() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << ispunct(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << ispunct(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to ispunct() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isspace(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isspace(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isspace() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isupper(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isupper(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isupper() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isxdigit(61) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << isxdigit(-61) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Passing value -61 to isxdigit() causes undefined behavior which may lead to a crash.\n", errout.str());
check("void f() {\n"
"std::isgraph(-10000, loc);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Passing value -10000 to isgraph() causes undefined behavior which may lead to a crash.\n", errout.str());
}
void switchRedundantAssignmentTest() {
check("void foo()\n"