Fixed #5980 (ValueFlow: pointer cant be 0 below throw in switch)

This commit is contained in:
Daniel Marjamäki 2015-02-07 10:45:30 +01:00
parent 977fbc8030
commit 29db3eb848
2 changed files with 26 additions and 4 deletions

View File

@ -236,17 +236,28 @@ static bool bailoutSelfAssignment(const Token * const tok)
static bool isReturn(const Token *tok)
{
const Token *prev = tok ? tok->previous() : nullptr;
if (Token::simpleMatch(prev, "}") && Token::simpleMatch(prev->link()->tokAt(-2),"} else {"))
return isReturn(prev) && isReturn(prev->link()->tokAt(-2));
if (Token::Match(prev ? prev->previous() : nullptr, "} ;"))
prev = prev->previous();
if (Token::simpleMatch(prev, "}")) {
if (Token::simpleMatch(prev->link()->tokAt(-2), "} else {"))
return isReturn(prev) && isReturn(prev->link()->tokAt(-2));
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
!Token::findsimplematch(prev->link(), "break", prev)) {
return true;
}
}
if (Token::simpleMatch(prev, ";")) {
// noreturn function
if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% ("))
return true;
// return/goto statement
prev = prev->previous();
while (prev && !Token::Match(prev,"[;{}]"))
while (prev && !Token::Match(prev, ";|{|}|return|goto|throw"))
prev = prev->previous();
return Token::Match(prev, "[;{}] return|goto");
return prev && prev->isName();
}
return false;
}

View File

@ -862,6 +862,17 @@ private:
"}";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 3));
code = "int f(int *x) {\n" // #5980
" if (!x) {\n"
" switch (i) {\n"
" default:\n"
" throw std::runtime_error(msg);\n"
" };\n"
" }\n"
" return *x;\n" // <- x is not 0
"}";
ASSERT_EQUALS(false, testValueOfX(code, 8U, 0));
// pointer/reference to x
code = "int f(void) {\n"
" int x = 2;\n"