From 7d84b8f1bc03d234dc586e906b69edbb5da3193d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 May 2021 16:13:32 +0200 Subject: [PATCH] Uninitialized variables; Fix FP when assigning struct members --- lib/checkuninitvar.cpp | 4 ++++ test/testuninitvar.cpp | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index e32400738..65c1da3b5 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1218,6 +1218,10 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, // * Passing address in RHS to pointer variable { const Token *tok = derefValue ? derefValue : valueExpr; + if (alloc == NO_ALLOC) { + while (tok->valueType() && tok->valueType()->pointer == 0 && Token::simpleMatch(tok->astParent(), ".")) + tok = tok->astParent(); + } if (Token::simpleMatch(tok->astParent(), "=")) { if (astIsLhs(tok)) return nullptr; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 5acb6289c..443a7ec25 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3516,6 +3516,15 @@ private: "}\n", "test.c"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("struct Cstring { char *text; int size, alloc; };\n" + "int maybe();\n" + "void f() {\n" + " Cstring res;\n" + " if ( ! maybe() ) return;\n" // <- fp goes away if this is removed + " ( ((res).text = (void*)0), ((res).size = (res).alloc = 0) );\n" // <- fp goes away if parentheses are removed + "}"); + ASSERT_EQUALS("", errout.str()); + { const char argDirectionsTestXmlData[] = "\n" "\n"