Skip to content

Commit bdc024b

Browse files
committed
Fixed 6634 (false positive: dereferencing null pointer in a sizeof operation)
1 parent 2681b0e commit bdc024b

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎lib/checknullpointer.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
312312
const bool printInconclusive = (_settings->inconclusive);
313313

314314
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
315+
if (Token::Match(tok, "sizeof|decltype|typeid|typeof (")) {
316+
tok = tok->next()->link();
317+
continue;
318+
}
319+
315320
const Variable *var = tok->variable();
316321
if (!var || !var->isPointer() || tok == var->nameToken())
317322
continue;

‎test/testnullpointer.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,13 @@ class TestNullPointer : public TestFixture {
17371737
" *p = 0;\n"
17381738
"}");
17391739
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
1740+
1741+
// sizeof
1742+
check("void f() {\n"
1743+
" int *pointer = NULL;\n"
1744+
" pointer = func(sizeof pointer[0]);\n"
1745+
"}", false, "test.cpp", false);
1746+
ASSERT_EQUALS("", errout.str());
17401747
}
17411748

17421749
// Test CheckNullPointer::nullConstantDereference

0 commit comments

Comments
 (0)