Fixed #890 (false positive: Uninitialized variable when pointer takes the address to array)
This commit is contained in:
parent
7fc72484ec
commit
797e9aeaf5
|
@ -1180,7 +1180,7 @@ void CheckOther::nullPointer()
|
||||||
nullPointerConditionalAssignment();
|
nullPointerConditionalAssignment();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Token *uninitvar_checkscope(const Token *tok, const unsigned int varid, bool &init, const bool pointer, const bool array)
|
static const Token *uninitvar_checkscope(const Token * const tokens, const Token *tok, const unsigned int varid, bool &init, const bool pointer, const bool array)
|
||||||
{
|
{
|
||||||
/* limit the checking in conditional code..
|
/* limit the checking in conditional code..
|
||||||
* int x;
|
* int x;
|
||||||
|
@ -1238,7 +1238,7 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
|
||||||
|
|
||||||
// Recursively check into the if ..
|
// Recursively check into the if ..
|
||||||
bool init2 = false;
|
bool init2 = false;
|
||||||
const Token *tokerr = uninitvar_checkscope(tok->next(), varid, init2, pointer, array);
|
const Token *tokerr = uninitvar_checkscope(tokens, tok->next(), varid, init2, pointer, array);
|
||||||
if (!limit && tokerr)
|
if (!limit && tokerr)
|
||||||
return tokerr;
|
return tokerr;
|
||||||
|
|
||||||
|
@ -1263,7 +1263,7 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
|
||||||
|
|
||||||
// there is no "if"..
|
// there is no "if"..
|
||||||
init2 = false;
|
init2 = false;
|
||||||
tokerr = uninitvar_checkscope(tok->next(), varid, init2, pointer, array);
|
tokerr = uninitvar_checkscope(tokens, tok->next(), varid, init2, pointer, array);
|
||||||
if (!limit && tokerr)
|
if (!limit && tokerr)
|
||||||
return tokerr;
|
return tokerr;
|
||||||
|
|
||||||
|
@ -1302,15 +1302,22 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
|
||||||
return tok->tokAt(2);
|
return tok->tokAt(2);
|
||||||
if (Token::Match(tok, "strcpy|strcat|strncpy|strncat|memcpy ( %any% , %varid% [,)]", varid))
|
if (Token::Match(tok, "strcpy|strcat|strncpy|strncat|memcpy ( %any% , %varid% [,)]", varid))
|
||||||
return tok->tokAt(4);
|
return tok->tokAt(4);
|
||||||
|
if (Token::Match(tok, "strcat|strncat ( %varid% ,", varid))
|
||||||
|
return tok->tokAt(3);
|
||||||
|
if (Token::Match(tok, "asm ( )"))
|
||||||
|
{
|
||||||
|
init = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// is the variable passed as a parameter to some function?
|
// is the variable passed as a parameter to some function?
|
||||||
unsigned int parlevel = 0;
|
unsigned int parlevel = 0;
|
||||||
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "{")
|
if (tok2->str() == "(")
|
||||||
++parlevel;
|
++parlevel;
|
||||||
|
|
||||||
else if (tok2->str() == "}")
|
else if (tok2->str() == ")")
|
||||||
{
|
{
|
||||||
if (parlevel <= 1)
|
if (parlevel <= 1)
|
||||||
break;
|
break;
|
||||||
|
@ -1332,7 +1339,18 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
|
||||||
return tok;
|
return tok;
|
||||||
|
|
||||||
if (Token::simpleMatch(tok->previous(), "="))
|
if (Token::simpleMatch(tok->previous(), "="))
|
||||||
return tok;
|
{
|
||||||
|
if (!Token::Match(tok->tokAt(-3), "[;{}] %var% ="))
|
||||||
|
return tok;
|
||||||
|
|
||||||
|
const unsigned int varid2 = tok->tokAt(-2)->varId();
|
||||||
|
if (varid2)
|
||||||
|
{
|
||||||
|
const Token *tok2 = Token::findmatch(tokens, "%varid%", varid2);
|
||||||
|
if (tok2 && !Token::simpleMatch(tok2->previous(), "*"))
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (pointer && Token::simpleMatch(tok->next(), "."))
|
if (pointer && Token::simpleMatch(tok->next(), "."))
|
||||||
return tok;
|
return tok;
|
||||||
|
@ -1391,7 +1409,7 @@ void CheckOther::uninitvar()
|
||||||
|
|
||||||
// check if variable is accessed uninitialized..
|
// check if variable is accessed uninitialized..
|
||||||
bool init = false;
|
bool init = false;
|
||||||
const Token *tokerr = uninitvar_checkscope(tok->next(), tok->varId(), init, pointer, array);
|
const Token *tokerr = uninitvar_checkscope(_tokenizer->tokens(), tok->next(), tok->varId(), init, pointer, array);
|
||||||
if (tokerr)
|
if (tokerr)
|
||||||
uninitvarError(tokerr, tok->str());
|
uninitvarError(tokerr, tok->str());
|
||||||
}
|
}
|
||||||
|
|
|
@ -980,6 +980,13 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar("void a()\n"
|
||||||
|
"{\n"
|
||||||
|
" int x[10];\n"
|
||||||
|
" int *y = x;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkUninitVar("int a()\n"
|
checkUninitVar("int a()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int ret;\n"
|
" int ret;\n"
|
||||||
|
@ -1077,6 +1084,13 @@ private:
|
||||||
" strcpy(s2, s);\n"
|
" strcpy(s2, s);\n"
|
||||||
"};\n");
|
"};\n");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar("void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" char s[20];\n"
|
||||||
|
" strcat(s, \"abc\");\n"
|
||||||
|
"};\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue