From 899b88754f74749c67793e4fd8a1bf259e1c04b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 15 May 2021 11:16:32 +0200 Subject: [PATCH] uninitialized variables; fix false positive in loop --- lib/checkuninitvar.cpp | 8 +++++--- test/testuninitvar.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) 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..