X Tutup
Skip to content

Commit fe8d04e

Browse files
committed
CheckNullPointer: Fix FP when x is NULL and address is calculated with expression '&x->y.z[0]'
1 parent c546776 commit fe8d04e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/checknullpointer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown)
171171
if (parent->str() == "[" && (!parent->astParent() || parent->astParent()->str() != "&"))
172172
return true;
173173

174+
// address of member variable / array element
175+
const Token *parent2 = parent;
176+
while (Token::Match(parent2, "[|."))
177+
parent2 = parent2->astParent();
178+
if (parent2 != parent && parent2 && parent2->str() == "&" && !parent2->astOperand2())
179+
return false;
180+
174181
// read/write member variable
175182
if (firstOperand && parent->str() == "." && (!parent->astParent() || parent->astParent()->str() != "&")) {
176183
if (!parent->astParent() || parent->astParent()->str() != "(" || parent->astParent() == tok->previous())

test/testnullpointer.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TestNullPointer : public TestFixture {
6060
TEST_CASE(nullpointer25); // #5061
6161
TEST_CASE(nullpointer26); // #3589
6262
TEST_CASE(nullpointer27); // #6014
63+
TEST_CASE(nullpointer_addressOf); // address of
6364
TEST_CASE(nullpointerSwitch); // #2626
6465
TEST_CASE(nullpointer_cast); // #4692
6566
TEST_CASE(nullpointer_castToVoid); // #3771
@@ -1335,6 +1336,20 @@ class TestNullPointer : public TestFixture {
13351336
"[test.cpp:3]: (error) Null pointer dereference\n", errout.str());
13361337
}
13371338

1339+
void nullpointer_addressOf() { // address of
1340+
check("void f() {\n"
1341+
" struct X *x = 0;\n"
1342+
" if (addr == &x->y) {}\n"
1343+
"}");
1344+
ASSERT_EQUALS("", errout.str());
1345+
1346+
check("void f() {\n"
1347+
" struct X *x = 0;\n"
1348+
" if (addr == &x->y.z[0]) {}\n"
1349+
"}");
1350+
ASSERT_EQUALS("", errout.str());
1351+
}
1352+
13381353
void nullpointerSwitch() { // #2626
13391354
check("char *f(int x) {\n"
13401355
" char *p = do_something();\n"

0 commit comments

Comments
 (0)
X Tutup