Check subtraction of pointers to different objects
This commit is contained in:
parent
0e988cc755
commit
e846312fed
|
@ -2864,7 +2864,7 @@ void CheckOther::checkComparePointers()
|
||||||
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
|
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
|
||||||
for (const Scope *functionScope : symbolDatabase->functionScopes) {
|
for (const Scope *functionScope : symbolDatabase->functionScopes) {
|
||||||
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
|
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
|
||||||
if (!Token::Match(tok, "<|>|<=|>="))
|
if (!Token::Match(tok, "<|>|<=|>=|-"))
|
||||||
continue;
|
continue;
|
||||||
const Token *tok1 = tok->astOperand1();
|
const Token *tok1 = tok->astOperand1();
|
||||||
const Token *tok2 = tok->astOperand2();
|
const Token *tok2 = tok->astOperand2();
|
||||||
|
@ -2892,6 +2892,9 @@ void CheckOther::checkComparePointers()
|
||||||
void CheckOther::comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2)
|
void CheckOther::comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2)
|
||||||
{
|
{
|
||||||
ErrorPath errorPath;
|
ErrorPath errorPath;
|
||||||
|
std::string verb = "Comparing";
|
||||||
|
if (Token::simpleMatch(tok, "-"))
|
||||||
|
verb = "Subtracting";
|
||||||
if (v1) {
|
if (v1) {
|
||||||
errorPath.emplace_back(v1->tokvalue->variable()->nameToken(), "Variable declared here.");
|
errorPath.emplace_back(v1->tokvalue->variable()->nameToken(), "Variable declared here.");
|
||||||
errorPath.insert(errorPath.end(), v1->errorPath.begin(), v1->errorPath.end());
|
errorPath.insert(errorPath.end(), v1->errorPath.begin(), v1->errorPath.end());
|
||||||
|
@ -2902,5 +2905,5 @@ void CheckOther::comparePointersError(const Token *tok, const ValueFlow::Value *
|
||||||
}
|
}
|
||||||
errorPath.emplace_back(tok, "");
|
errorPath.emplace_back(tok, "");
|
||||||
reportError(
|
reportError(
|
||||||
errorPath, Severity::error, "comparePointers", "Comparing pointers that point to different objects", CWE570, false);
|
errorPath, Severity::error, "comparePointers", verb + " pointers that point to different objects", CWE570, false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7663,6 +7663,15 @@ private:
|
||||||
"[test.cpp:1] -> [test.cpp:2] -> [test.cpp:1] -> [test.cpp:2] -> [test.cpp:2]: (error) Comparing pointers that point to different objects\n",
|
"[test.cpp:1] -> [test.cpp:2] -> [test.cpp:1] -> [test.cpp:2] -> [test.cpp:2]: (error) Comparing pointers that point to different objects\n",
|
||||||
errout.str());
|
errout.str());
|
||||||
|
|
||||||
|
check("int f() {\n"
|
||||||
|
" int x = 0;\n"
|
||||||
|
" int y = 1;\n"
|
||||||
|
" return &x - &y;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS(
|
||||||
|
"[test.cpp:2] -> [test.cpp:4] -> [test.cpp:3] -> [test.cpp:4] -> [test.cpp:4]: (error) Subtracting pointers that point to different objects\n",
|
||||||
|
errout.str());
|
||||||
|
|
||||||
check("bool f() {\n"
|
check("bool f() {\n"
|
||||||
" int x[2] = {1, 2}m;\n"
|
" int x[2] = {1, 2}m;\n"
|
||||||
" int* xp = &x[0];\n"
|
" int* xp = &x[0];\n"
|
||||||
|
|
Loading…
Reference in New Issue