Fixed #1023 (improve check: Unintialized variable not detected when using +=)
This commit is contained in:
parent
e6d5c76138
commit
e248f7d3e5
|
@ -1625,6 +1625,26 @@ private:
|
||||||
return &tok;
|
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 ["))
|
if (Token::Match(tok.next(), "= malloc|kmalloc") || Token::simpleMatch(tok.next(), "= new char ["))
|
||||||
{
|
{
|
||||||
alloc_pointer(checks, tok.varId());
|
alloc_pointer(checks, tok.varId());
|
||||||
|
|
|
@ -1184,6 +1184,28 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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..
|
// goto..
|
||||||
checkUninitVar("void foo(int x)\n"
|
checkUninitVar("void foo(int x)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
|
Loading…
Reference in New Issue