From d36aa590cdd499d2f41e42c79dec607a91c0edf5 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Thu, 23 Dec 2021 01:22:41 -0600 Subject: [PATCH] Fix 10553: FP uninitvar with strcpy (#3652) --- lib/astutils.cpp | 13 ++++++++++++- test/testuninitvar.cpp | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 3817e16c3..eb11d28a1 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2017,6 +2017,17 @@ static bool isTrivialConstructor(const Token* tok) return false; } +static bool isArray(const Token* tok) +{ + if (!tok) + return false; + if (tok->variable()) + return tok->variable()->isArray(); + if (Token::simpleMatch(tok, ".")) + return isArray(tok->astOperand2()); + return false; +} + bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive) { if (!tok) @@ -2056,7 +2067,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti argDirection == Library::ArgumentChecks::Direction::DIR_INOUT) { // With out or inout the direction of the content is specified, not a pointer itself, so ignore pointers for now const ValueType * const valueType = tok1->valueType(); - if (valueType && valueType->pointer == indirect) { + if ((valueType && valueType->pointer == indirect) || (indirect == 0 && isArray(tok1))) { return true; } } diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 04a902fbb..769b528b4 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -5129,6 +5129,17 @@ private: " return rez;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #10553 + valueFlowUninit("struct CharDataOnly {\n" + " char data[100];\n" + "};\n" + "CharDataOnly f() {\n" + " CharDataOnly testData;\n" + " strcpy(testData.data, \"string smaller than size\");\n" + " return testData;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value