Fix #3278 (FP: Possible null pointer dereference with for loop)

http://sourceforge.net/apps/trac/cppcheck/ticket/3278
Bail out if token "for" is encountered.
This commit is contained in:
Reijo Tomperi 2011-11-07 22:10:21 +02:00
parent 5cf7cb9ff5
commit 7b7e072b88
2 changed files with 12 additions and 1 deletions

View File

@ -795,7 +795,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
}
}
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) {
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch|for")) {
bool dummy = false;
if (Token::Match(tok2, "return * %varid%", varid))
nullPointerError(tok2, pointerName, linenr, inconclusive);

View File

@ -56,6 +56,7 @@ private:
TEST_CASE(scanf_with_invalid_va_argument);
TEST_CASE(nullpointer_in_return);
TEST_CASE(nullpointer_in_typeid);
TEST_CASE(nullpointer_in_for_loop);
}
void check(const char code[], bool inconclusive = false, bool cpp11 = false) {
@ -1576,6 +1577,16 @@ private:
}
void nullpointer_in_for_loop() {
// Ticket #3278
check("void f(int* ptr, int cnt){\n"
" if (!ptr)\n"
" cnt = 0;\n"
" for (int i = 0; i < cnt; ++i)\n"
" *ptr++ = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestNullPointer)