Fixed #4773 (Regression: wrong detection of unitialized variable)
This commit is contained in:
parent
4659745106
commit
bca751b9f4
|
@ -1241,7 +1241,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
std::map<unsigned int, int> varValueIf;
|
std::map<unsigned int, int> varValueIf;
|
||||||
if (!initif && !noreturnIf) {
|
if (!alwaysFalse && !initif && !noreturnIf) {
|
||||||
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
||||||
if (Token::Match(tok2, "[;{}.] %var% = - %var% ;"))
|
if (Token::Match(tok2, "[;{}.] %var% = - %var% ;"))
|
||||||
varValueIf[tok2->next()->varId()] = NOT_ZERO;
|
varValueIf[tok2->next()->varId()] = NOT_ZERO;
|
||||||
|
@ -1268,10 +1268,10 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
||||||
|
|
||||||
bool possibleInitElse(number_of_if > 0 || suppressErrors);
|
bool possibleInitElse(number_of_if > 0 || suppressErrors);
|
||||||
bool noreturnElse = false;
|
bool noreturnElse = false;
|
||||||
const bool initelse = checkScopeForVariable(scope, tok->next(), var, &possibleInitElse, NULL, membervar);
|
const bool initelse = !alwaysTrue && checkScopeForVariable(scope, tok->next(), var, &possibleInitElse, NULL, membervar);
|
||||||
|
|
||||||
std::map<unsigned int, int> varValueElse;
|
std::map<unsigned int, int> varValueElse;
|
||||||
if (!initelse && !noreturnElse) {
|
if (!alwaysTrue && !initelse && !noreturnElse) {
|
||||||
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
for (const Token *tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
|
||||||
if (Token::Match(tok2, "[;{}.] %var% = - %var% ;"))
|
if (Token::Match(tok2, "[;{}.] %var% = - %var% ;"))
|
||||||
varValueElse[tok2->next()->varId()] = NOT_ZERO;
|
varValueElse[tok2->next()->varId()] = NOT_ZERO;
|
||||||
|
@ -1286,10 +1286,8 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
|
||||||
// goto the }
|
// goto the }
|
||||||
tok = tok->link();
|
tok = tok->link();
|
||||||
|
|
||||||
if (initif && initelse)
|
if ((alwaysFalse || initif || noreturnIf) &&
|
||||||
return true;
|
(alwaysTrue || initelse || noreturnElse))
|
||||||
|
|
||||||
if ((initif && noreturnElse) || (initelse && noreturnIf))
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if ((initif || initelse || possibleInitElse) && !noreturnIf && !noreturnElse) {
|
if ((initif || initelse || possibleInitElse) && !noreturnIf && !noreturnElse) {
|
||||||
|
|
|
@ -2261,11 +2261,41 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkUninitVar2("int* f(int a) {\n" // #4560
|
checkUninitVar2("int f(int a) {\n" // #4560
|
||||||
" int x = 0, y;\n"
|
" int x = 0, y;\n"
|
||||||
" if (a) x = 1;\n"
|
" if (a) x = 1;\n"
|
||||||
" else return 0;\n"
|
" else return 0;\n"
|
||||||
" if (x) y = 123;\n" // <- y is always initialized
|
" if (x) y = 123;\n" // <- y is always initialized
|
||||||
|
" else {}\n"
|
||||||
|
" return y;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar2("int f(int a) {\n" // #4560
|
||||||
|
" int x = 1, y;\n"
|
||||||
|
" if (a) x = 0;\n"
|
||||||
|
" else return 0;\n"
|
||||||
|
" if (x) {}\n"
|
||||||
|
" else y = 123;\n" // <- y is always initialized
|
||||||
|
" return y;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar2("static int x;" // #4773
|
||||||
|
"int f() {\n"
|
||||||
|
" int y;\n"
|
||||||
|
" if (x) g();\n"
|
||||||
|
" if (x) y = 123;\n"
|
||||||
|
" else y = 456;\n"
|
||||||
|
" return y;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar2("static int x;" // #4773
|
||||||
|
"int f() {\n"
|
||||||
|
" int y;\n"
|
||||||
|
" if (!x) g();\n"
|
||||||
|
" if (x) y = 123;\n"
|
||||||
" else y = 456;\n"
|
" else y = 456;\n"
|
||||||
" return y;\n"
|
" return y;\n"
|
||||||
"}");
|
"}");
|
||||||
|
|
Loading…
Reference in New Issue