diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index e2cdf8abf..e066901f2 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -858,9 +858,11 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va if (tok->str() == "{") { const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout); - if (Token::simpleMatch(tok->link(), "} else {")) { - const Token *elseBody = tok->link()->tokAt(2); + tok = tok->link(); + if (Token::simpleMatch(tok, "} else {")) { + const Token *elseBody = tok->tokAt(2); const Token *errorToken2 = checkLoopBodyRecursive(elseBody, var, alloc, membervar, bailout); + tok = elseBody->link(); if (errorToken1 && errorToken2) return errorToken1; if (errorToken2) @@ -868,7 +870,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va } if (bailout) return nullptr; - if (errorToken1) + if (!errorToken) errorToken = errorToken1; } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9d39a1795..4cffef578 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1303,6 +1303,20 @@ private: " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: nextEntry\n", errout.str()); + + checkUninitVar("void f(int x) {\n" + " list *f = NULL;\n" + " list *l;\n" + "\n" + " while (--x) {\n" + " if (!f)\n" + " f = c;\n" + " else\n" + " l->next = c;\n" // <- not uninitialized + " l = c;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // switch..