From 809c70b9c3e0bbdaa05bf2a823f5677a1bb78356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 14 May 2021 21:36:51 +0200 Subject: [PATCH] uninitialized variables; fix FP in for loop --- lib/checkuninitvar.cpp | 20 ++++++++++++++++++++ test/testuninitvar.cpp | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 522d40017..f61039ce9 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -836,6 +836,26 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va return nullptr; } + // for loop; skip third expression until loop body has been analyzed.. + if (tok->str() == ";" && Token::simpleMatch(tok->astParent(), ";") && Token::simpleMatch(tok->astParent()->astParent(), "(")) { + const Token *top = tok->astParent()->astParent(); + if (!Token::simpleMatch(top->previous(), "for (") || !Token::simpleMatch(top->link(), ") {")) + continue; + const Token *bodyStart = top->link()->next(); + const Token *errorToken = checkLoopBodyRecursive(bodyStart, var, alloc, membervar, bailout); + if (errorToken) + return errorToken; + if (bailout) + return nullptr; + } + // for loop; skip loop body if there is third expression + if (Token::simpleMatch(tok, ") {") && + Token::simpleMatch(tok->link()->previous(), "for (") && + Token::simpleMatch(tok->link()->astOperand2(), ";") && + Token::simpleMatch(tok->link()->astOperand2()->astOperand2(), ";")) { + tok = tok->linkAt(1); + } + if (tok->str() == "{") { const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout); if (Token::simpleMatch(tok->link(), "} else {")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 3e7d99b16..66d1fda6f 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1283,6 +1283,25 @@ private: " }\n" "}"); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: learn\n", errout.str()); + + checkUninitVar("void foo() {\n" + " Entry *entry, *nextEntry;\n" + " for(int i = 0; i < 10; i++) {\n" + " for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n" // <- nextEntry is not uninitialized + " nextEntry = entry->next;\n" + " }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + checkUninitVar("void foo() {\n" + " Entry *entry, *nextEntry;\n" + " for(int i = 0; i < 10; i++) {\n" + " for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n" + " }\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: nextEntry\n", errout.str()); } // switch..