Skip to content

Commit

Permalink
Fix #12422 False positive: subtracting pointers in same struct
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Feb 10, 2024
1 parent a7086c5 commit b53cd50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3801,6 +3801,8 @@ void CheckOther::checkComparePointers()
continue;
if (v1.tokvalue->varId() == v2.tokvalue->varId())
continue;
if (var1->scope() == var2->scope() && var1->scope()->isClassOrStructOrUnion())
continue;
if (var1->isReference() || var2->isReference())
continue;
if (var1->isRValueReference() || var2->isRValueReference())
Expand Down
10 changes: 9 additions & 1 deletion test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11549,7 +11549,6 @@ class TestOther : public TestFixture {
" return xp > yp;\n"
"}");
ASSERT_EQUALS(
"[test.cpp:1] -> [test.cpp:5] -> [test.cpp:1] -> [test.cpp:6] -> [test.cpp:7]: (error) Comparing pointers that point to different objects\n"
"[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n"
"[test.cpp:6]: (style) Variable 'yp' can be declared as pointer to const\n"
"[test.cpp:5]: (style) Variable 'xp' can be declared as pointer to const\n" // duplicate
Expand Down Expand Up @@ -11646,6 +11645,15 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:1] -> [test.cpp:3] -> [test.cpp:2] -> [test.cpp:3] -> [test.cpp:3]: (error) Subtracting pointers that point to different objects\n",
errout.str());

check("struct S {\n" // #12422
" int a, b;\n"
"};\n"
"void f() {\n"
" S s{};\n"
" size_t x = static_cast<size_t>(reinterpret_cast<char*>(&s.b) - reinterpret_cast<char*>(&s.a));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void unusedVariableValueTemplate() {
Expand Down

0 comments on commit b53cd50

Please sign in to comment.