Use ValueFlow for compareBoolExpressionWithInt
This commit is contained in:
parent
0352a5d32f
commit
b09bcdc38c
|
@ -333,26 +333,29 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
|
||||||
if (astIsBool(numTok))
|
if (astIsBool(numTok))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (numTok->isNumber()) {
|
const ValueFlow::Value *minval = numTok->getValueLE(0, mSettings);
|
||||||
const MathLib::bigint num = MathLib::toLongNumber(numTok->str());
|
if (minval && minval->intvalue == 0 &&
|
||||||
if (num==0 &&
|
|
||||||
(numInRhs ? Token::Match(tok, ">|==|!=")
|
(numInRhs ? Token::Match(tok, ">|==|!=")
|
||||||
: Token::Match(tok, "<|==|!=")))
|
: Token::Match(tok, "<|==|!=")))
|
||||||
continue;
|
minval = nullptr;
|
||||||
if (num==1 &&
|
|
||||||
|
const ValueFlow::Value *maxval = numTok->getValueGE(1, mSettings);
|
||||||
|
if (maxval && maxval->intvalue == 1 &&
|
||||||
(numInRhs ? Token::Match(tok, "<|==|!=")
|
(numInRhs ? Token::Match(tok, "<|==|!=")
|
||||||
: Token::Match(tok, ">|==|!=")))
|
: Token::Match(tok, ">|==|!=")))
|
||||||
continue;
|
maxval = nullptr;
|
||||||
comparisonOfBoolExpressionWithIntError(tok, true);
|
|
||||||
} else if (astIsIntegral(numTok, false) && mTokenizer->isCPP())
|
if (minval || maxval) {
|
||||||
comparisonOfBoolExpressionWithIntError(tok, false);
|
bool not0or1 = (minval && minval->intvalue < 0) || (maxval && maxval->intvalue > 1);
|
||||||
|
comparisonOfBoolExpressionWithIntError(tok, not0or1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckBool::comparisonOfBoolExpressionWithIntError(const Token *tok, bool n0o1)
|
void CheckBool::comparisonOfBoolExpressionWithIntError(const Token *tok, bool not0or1)
|
||||||
{
|
{
|
||||||
if (n0o1)
|
if (not0or1)
|
||||||
reportError(tok, Severity::warning, "compareBoolExpressionWithInt",
|
reportError(tok, Severity::warning, "compareBoolExpressionWithInt",
|
||||||
"Comparison of a boolean expression with an integer other than 0 or 1.", CWE398, false);
|
"Comparison of a boolean expression with an integer other than 0 or 1.", CWE398, false);
|
||||||
else
|
else
|
||||||
|
|
|
@ -107,7 +107,7 @@ private:
|
||||||
void assignBoolToPointerError(const Token *tok);
|
void assignBoolToPointerError(const Token *tok);
|
||||||
void assignBoolToFloatError(const Token *tok);
|
void assignBoolToFloatError(const Token *tok);
|
||||||
void bitwiseOnBooleanError(const Token *tok, const std::string &expression, const std::string &op);
|
void bitwiseOnBooleanError(const Token *tok, const std::string &expression, const std::string &op);
|
||||||
void comparisonOfBoolExpressionWithIntError(const Token *tok, bool n0o1);
|
void comparisonOfBoolExpressionWithIntError(const Token *tok, bool not0or1);
|
||||||
void pointerArithBoolError(const Token *tok);
|
void pointerArithBoolError(const Token *tok);
|
||||||
void returnValueBoolError(const Token *tok);
|
void returnValueBoolError(const Token *tok);
|
||||||
|
|
||||||
|
|
|
@ -308,7 +308,7 @@ private:
|
||||||
" a++;\n"
|
" a++;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
);
|
);
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
||||||
|
|
||||||
check("void f(int x) {\n"
|
check("void f(int x) {\n"
|
||||||
" if ((5 && x) < 1)\n"
|
" if ((5 && x) < 1)\n"
|
||||||
|
@ -322,7 +322,7 @@ private:
|
||||||
" a++;\n"
|
" a++;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
);
|
);
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
||||||
|
|
||||||
|
|
||||||
check("void f(int x) {\n"
|
check("void f(int x) {\n"
|
||||||
|
@ -337,7 +337,7 @@ private:
|
||||||
" a++;\n"
|
" a++;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
);
|
);
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
||||||
|
|
||||||
check("void f(int x) {\n"
|
check("void f(int x) {\n"
|
||||||
" if (1 > (5 && x))\n"
|
" if (1 > (5 && x))\n"
|
||||||
|
@ -351,7 +351,7 @@ private:
|
||||||
" a++;\n"
|
" a++;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
);
|
);
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
||||||
|
|
||||||
check("void f(bool x ) {\n"
|
check("void f(bool x ) {\n"
|
||||||
" if ( x > false )\n"
|
" if ( x > false )\n"
|
||||||
|
@ -422,16 +422,6 @@ private:
|
||||||
check("int f() { return (!a+b<c); }");
|
check("int f() { return (!a+b<c); }");
|
||||||
ASSERT_EQUALS("",errout.str());
|
ASSERT_EQUALS("",errout.str());
|
||||||
|
|
||||||
{
|
|
||||||
const char code[] = "void f(int x, bool y) { if ( x != y ) {} }";
|
|
||||||
|
|
||||||
check(code, false, "test.cpp");
|
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
|
||||||
|
|
||||||
check(code, false, "test.c");
|
|
||||||
ASSERT_EQUALS("", errout.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
check("int f() { return (a+(b<5)<=c); }");
|
check("int f() { return (a+(b<5)<=c); }");
|
||||||
ASSERT_EQUALS("",errout.str());
|
ASSERT_EQUALS("",errout.str());
|
||||||
}
|
}
|
||||||
|
@ -513,7 +503,7 @@ private:
|
||||||
ASSERT_EQUALS("",errout.str());
|
ASSERT_EQUALS("",errout.str());
|
||||||
|
|
||||||
check("void f(int a, int b, int c) { if (1 < !(a+b)) {} }");
|
check("void f(int a, int b, int c) { if (1 < !(a+b)) {} }");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n",errout.str());
|
ASSERT_EQUALS("[test.cpp:1]: (warning) Comparison of a boolean expression with an integer.\n",errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void comparisonOfBoolExpressionWithInt3() {
|
void comparisonOfBoolExpressionWithInt3() {
|
||||||
|
@ -533,7 +523,7 @@ private:
|
||||||
check("void f(int a, int b, int c) {\n"
|
check("void f(int a, int b, int c) {\n"
|
||||||
" return (a > b) < c;\n"
|
" return (a > b) < c;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("void f(int a, int b, int c) {\n"
|
check("void f(int a, int b, int c) {\n"
|
||||||
" return x(a > b) < c;\n"
|
" return x(a > b) < c;\n"
|
||||||
|
@ -629,14 +619,8 @@ private:
|
||||||
" printf(\"foo\");\n"
|
" printf(\"foo\");\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"bool compare(int temp){\n"
|
"bool compare(int temp);");
|
||||||
" if(temp==4){\n"
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n"
|
||||||
" return true;\n"
|
|
||||||
" }\n"
|
|
||||||
" else\n"
|
|
||||||
" return false;\n"
|
|
||||||
"}");
|
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of a boolean expression with an integer.\n"
|
|
||||||
"[test.cpp:3]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n", errout.str());
|
"[test.cpp:3]: (style) Comparison of a function returning boolean value using relational (<, >, <= or >=) operator.\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -950,14 +934,14 @@ private:
|
||||||
" printf(\"foo\");\n"
|
" printf(\"foo\");\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("void f(int x, bool y) {\n"
|
check("void f(int x, bool y) {\n"
|
||||||
" if (x == y) {\n"
|
" if (x == y) {\n"
|
||||||
" printf(\"foo\");\n"
|
" printf(\"foo\");\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("void f(bool x, bool y) {\n"
|
check("void f(bool x, bool y) {\n"
|
||||||
" if (x == y) {\n"
|
" if (x == y) {\n"
|
||||||
|
@ -980,15 +964,14 @@ private:
|
||||||
" printf(\"foo\");\n"
|
" printf(\"foo\");\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n"
|
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
|
||||||
"[test.cpp:2]: (warning) Comparison of a boolean value using relational operator (<, >, <= or >=).\n", errout.str());
|
|
||||||
|
|
||||||
check("void f(int y) {\n"
|
check("void f(int y) {\n"
|
||||||
" if (true == y) {\n"
|
" if (true == y) {\n"
|
||||||
" printf(\"foo\");\n"
|
" printf(\"foo\");\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("void f(bool y) {\n"
|
check("void f(bool y) {\n"
|
||||||
" if (y == true) {\n"
|
" if (y == true) {\n"
|
||||||
|
@ -1049,7 +1032,7 @@ private:
|
||||||
" if (expectedResult == res)\n"
|
" if (expectedResult == res)\n"
|
||||||
" throw 2;\n"
|
" throw 2;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("bool Fun();\n"
|
check("bool Fun();\n"
|
||||||
"void Test(bool expectedResult) {\n"
|
"void Test(bool expectedResult) {\n"
|
||||||
|
@ -1057,7 +1040,7 @@ private:
|
||||||
" if (5 + expectedResult == res)\n"
|
" if (5 + expectedResult == res)\n"
|
||||||
" throw 2;\n"
|
" throw 2;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
check("int Fun();\n"
|
check("int Fun();\n"
|
||||||
"void Test(bool expectedResult) {\n"
|
"void Test(bool expectedResult) {\n"
|
||||||
|
@ -1073,7 +1056,7 @@ private:
|
||||||
" if (expectedResult == res + 5)\n"
|
" if (expectedResult == res + 5)\n"
|
||||||
" throw 2;\n"
|
" throw 2;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void comparisonOfBoolWithInt9() { // #9304
|
void comparisonOfBoolWithInt9() { // #9304
|
||||||
|
|
Loading…
Reference in New Issue