From de2509f1ac1ff6d1a1f33ed8e675a12ccf5c85d1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 21 Jun 2022 00:12:11 +0200 Subject: [PATCH] Fix FP with complex array size expression (#4225) * Partial fix for #11137 FN: invalidFunctionArgStr printf argument * Typo * Remove , suppressions * Add suppresion, remove * Add suppressions * Fix FP with complex array size expression --- lib/checkfunctions.cpp | 4 ++-- test/testfunctions.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 1d6ce74b9..1c0ba58f6 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -144,9 +144,9 @@ void CheckFunctions::invalidFunctionUsage() && valueType && (valueType->type == ValueType::Type::CHAR || valueType->type == ValueType::Type::WCHAR_T)) { const Token* varTok = variable->declEndToken(); auto count = -1; // Find out explicitly set count, e.g.: char buf[3] = {...}. Variable 'count' is set to 3 then. - if (varTok && Token::simpleMatch(varTok->previous(), "]")) + if (varTok && Token::simpleMatch(varTok->astOperand1(), "[")) { - const Token* const countTok = varTok->tokAt(-2); + const Token* const countTok = varTok->astOperand1()->astOperand2(); if (countTok && countTok->hasKnownIntValue()) count = countTok->getKnownIntValue(); } diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 33d05ff82..550d869bd 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -723,6 +723,12 @@ private: " return wcslen(c);\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required.\n", errout.str()); + + check("void f(char* dest) {\n" + " char if_name[(IF_NAMESIZE > 21 ? IF_NAMESIZE : 21) + 1] = \"%\";\n" + " strcat(dest, if_name);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void mathfunctionCall_sqrt() {