Skip to content

Commit

Permalink
Fixed danmar#2134 (sizeof(*list) complains of null pointer dereference)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Marjamäki committed Oct 26, 2010
1 parent b6106dd commit 9370f55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@ void CheckOther::nullPointerByDeRefAndChec()

// Check that variable is a pointer..
const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid);
if (!Token::Match(decltok->tokAt(-3), "[;,(] %type% *"))
if (!Token::Match(decltok->tokAt(-3), "[{};,(] %type% *"))
continue;

for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous())
Expand All @@ -2516,7 +2516,7 @@ void CheckOther::nullPointerByDeRefAndChec()
break;
}
// dereference in function call
else if (Token::Match(tok1->tokAt(-2), "[(,] *"))
else if (Token::Match(tok1->tokAt(-3), "!!sizeof [(,] *"))
{
nullPointerError(tok1, varname, tok->linenr());
}
Expand Down
11 changes: 11 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,17 @@ class TestOther : public TestFixture
" if (a) { }\n"
"}\n");
ASSERT_EQUALS("", errout.str());

// ticket #2134 - sizeof doesn't dereference
checkNullPointer("void f() {\n"
" int c = 1;\n"
" int *list = NULL;\n"
" sizeof(*list);\n"
" if (!list)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());

}

void nullpointer2()
Expand Down

0 comments on commit 9370f55

Please sign in to comment.