Uninitialized variables: Avoid FP when using '?:'

This commit is contained in:
Daniel Marjamäki 2013-07-10 16:44:35 +02:00
parent bbf3db4d78
commit 8f332af849
2 changed files with 25 additions and 0 deletions

View File

@ -1386,6 +1386,12 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
else if (Token::Match(tok, "sizeof|typeof|offsetof|decltype ("))
tok = tok->linkAt(1);
else if (tok->str() == "?")
// TODO: False negatives when "?:" is used.
// Fix the tokenizer and then remove this bailout.
// The tokenizer should replace "return x?y:z;" with "if(x)return y;return z;"
return true;
tok = tok->next();
}

View File

@ -2634,6 +2634,25 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("int f(int x) {\n" // FP with ?:
" int a;\n"
" if (x)\n"
" a = p;\n"
" return x ? 2*a : 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// TODO: False negative when "?:" is used
// This should probably be fixed in the tokenizer by changing
// "return x?y:z;" to "if(x)return y;return z;"
checkUninitVar2("int f(int x) {\n"
" int a;\n"
" if (x)\n"
" a = p;\n"
" return y ? 2*a : 3*a;\n"
"}\n");
TODO_ASSERT_EQUALS("error", "", errout.str());
// Unknown => bail out..
checkUninitVar2("void f(int x) {\n"
" int i;\n"