Fixed #5981 (FP: nullPointer on repeated switch)

This commit is contained in:
Daniel Marjamäki 2014-07-16 09:12:56 +02:00
parent d41ef3a6d0
commit 5bdd197b01
2 changed files with 16 additions and 0 deletions

View File

@ -618,6 +618,9 @@ static bool valueFlowForward(Token * const startToken,
if (varusage) {
varusagelevel = indentlevel;
if (indentlevel < 0 && tok2->str() == "switch")
return false;
// TODO: don't check noreturn scopes
if (number_of_if > 0U || Token::findmatch(tok2, "%varid%", start, varid)) {
if (settings->debugwarnings)
@ -683,6 +686,7 @@ static bool valueFlowForward(Token * const startToken,
const Scope *scope = tok2->scope();
if (scope && scope->type == Scope::eSwitch) {
tok2 = const_cast<Token *>(scope->classEnd);
--indentlevel;
continue;
}
}

View File

@ -754,6 +754,18 @@ private:
"}";
ASSERT_EQUALS(true, testValueOfX(code, 7U, 12));
ASSERT_EQUALS(true, testValueOfX(code, 7U, 34));
code = "void f() {\n" // #5981
" int x;\n"
" switch (ab) {\n"
" case A: x = 12; break;\n"
" case B: x = 34; break;\n"
" }\n"
" switch (ab) {\n"
" case A: v = x; break;\n" // <- x is not 34
" }\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 8U, 34));
}
void valueFlowAfterCondition() {