Skip to content

Commit e5c4a73

Browse files
Fix #14715 FP invalidFunctionArgStr, buffer is nul-terminated in every path (cppcheck-opensource#8516)
1 parent 0fda84e commit e5c4a73

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/checkfunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void CheckFunctions::invalidFunctionUsage()
153153
// Is non-null terminated local variable of type char (e.g. char buf[] = {'x'};) ?
154154
if (variable && variable->isLocal()
155155
&& valueType && (valueType->type == ValueType::Type::CHAR || valueType->type == ValueType::Type::WCHAR_T)
156-
&& !isVariablesChanged(variable->declEndToken(), functionToken, 0 /*indirect*/, { variable }, *mSettings)) {
156+
&& !isVariablesChanged(variable->declEndToken(), functionToken, valueType->pointer, { variable }, *mSettings)) {
157157
const Token* varTok = variable->declEndToken();
158158
MathLib::bigint count = -1; // Find out explicitly set count, e.g.: char buf[3] = {...}. Variable 'count' is set to 3 then.
159159
if (varTok && Token::simpleMatch(varTok->astOperand1(), "["))

test/testfunctions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,13 @@ class TestFunctions : public TestFixture {
761761
"}\n");
762762
ASSERT_EQUALS("", errout_str());
763763

764+
check("char* f() {\n" // #14715
765+
" char a[3] = { 'a', 'b', 'c' };\n"
766+
" *a = 0;\n"
767+
" return strdup(a);\n"
768+
"}\n");
769+
ASSERT_EQUALS("", errout_str());
770+
764771
check("size_t f() { wchar_t x = L'x'; return wcslen(&x); }");
765772
ASSERT_EQUALS("[test.cpp:1:46]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required. [invalidFunctionArgStr]\n", errout_str());
766773

0 commit comments

Comments
 (0)