Fixed #1023 (improve check: Unintialized variable not detected when using +=)

This commit is contained in:
Daniel Marjamäki 2010-01-03 18:49:13 +01:00
parent e6d5c76138
commit e248f7d3e5
2 changed files with 42 additions and 0 deletions

View File

@ -1625,6 +1625,26 @@ private:
return &tok;
}
// += etc
if (Token::Match(tok.previous(), "[;{}]") || Token::Match(tok.tokAt(-2), "[;{}] *"))
{
// goto the equal..
const Token *eq = tok.next();
if (eq && eq->str() == "[" && eq->link() && eq->link()->next())
eq = eq->link()->next();
// is it X=
if (Token::Match(eq, "+=|-=|*=|/=|&=|^=") || eq->str() == "|=")
{
if (tok.previous()->str() == "*")
use_pointer(foundError, checks, &tok);
else if (tok.next()->str() == "[")
use_array(foundError, checks, &tok);
else
use(foundError, checks, &tok);
}
}
if (Token::Match(tok.next(), "= malloc|kmalloc") || Token::simpleMatch(tok.next(), "= new char ["))
{
alloc_pointer(checks, tok.varId());

View File

@ -1184,6 +1184,28 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
// +=
checkUninitVar("void f()\n"
"{\n"
" int c;\n"
" c += 2;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" char *s = malloc(100);\n"
" *s += 10;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Data is allocated but not initialized: s\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" int a[10];\n"
" a[0] += 10;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
// goto..
checkUninitVar("void foo(int x)\n"
"{\n"