Skip to content

Commit 5b9fa96

Browse files
Partial fix for #11137 FN: invalidFunctionArgStr printf argument (#4224)
* Partial fix for #11137 FN: invalidFunctionArgStr printf argument * Typo * Remove <strz>, suppressions * Add suppresion, remove <strz> * Add suppressions
1 parent ff50a01 commit 5b9fa96

6 files changed

Lines changed: 24 additions & 8 deletions

File tree

cfg/posix.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5279,7 +5279,6 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
52795279
<not-null/>
52805280
<not-uninit/>
52815281
<minsize type="argvalue" arg="2"/>
5282-
<strz/>
52835282
</arg>
52845283
<arg nr="2" direction="in">
52855284
<not-uninit/>

cfg/std.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5209,7 +5209,6 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
52095209
<leak-ignore/>
52105210
<!-- In case the 3rd argument is 0, the 1st argument is permitted to be a null pointer. (#6306) -->
52115211
<arg nr="1" direction="out">
5212-
<strz/>
52135212
<minsize type="argvalue" arg="3"/>
52145213
</arg>
52155214
<arg nr="2" direction="in">

lib/checkfunctions.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void CheckFunctions::invalidFunctionUsage()
141141
const Variable* const variable = argtok->variable();
142142
// Is non-null terminated local variable of type char (e.g. char buf[] = {'x'};) ?
143143
if (variable && variable->isLocal()
144-
&& valueType && valueType->type == ValueType::Type::CHAR) {
144+
&& valueType && (valueType->type == ValueType::Type::CHAR || valueType->type == ValueType::Type::WCHAR_T)) {
145145
const Token* varTok = variable->declEndToken();
146146
auto count = -1; // Find out explicitly set count, e.g.: char buf[3] = {...}. Variable 'count' is set to 3 then.
147147
if (varTok && Token::simpleMatch(varTok->previous(), "]"))
@@ -170,6 +170,13 @@ void CheckFunctions::invalidFunctionUsage()
170170
&& (count == -1 || (count > 0 && count <= charsUntilFirstZero))) {
171171
invalidFunctionArgStrError(argtok, functionToken->str(), argnr);
172172
}
173+
} else if (count > -1 && Token::Match(varTok, "= %str%")) {
174+
const Token* strTok = varTok->getValueTokenMinStrSize(mSettings);
175+
if (strTok) {
176+
const int strSize = Token::getStrArraySize(strTok);
177+
if (strSize > count)
178+
invalidFunctionArgStrError(argtok, functionToken->str(), argnr);
179+
}
173180
}
174181
}
175182
}

test/cfg/posix.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,7 @@ void bufferAccessOutOfBounds_bzero(void *s, size_t n)
558558
size_t bufferAccessOutOfBounds_strnlen(const char *s, size_t maxlen)
559559
{
560560
const char buf[2]={'4','2'};
561-
// cppcheck-suppress invalidFunctionArgStr
562561
size_t len = strnlen(buf,2);
563-
// cppcheck-suppress invalidFunctionArgStr
564562
// cppcheck-suppress bufferAccessOutOfBounds
565563
len+=strnlen(buf,3);
566564
return len;

test/cfg/std.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ size_t invalidFunctionArgStr_wcslen(void)
3434
const wchar_t terminated0[] = L"ABCDEF49620910";
3535
const wchar_t terminated1[3] = { L'a', L'b', L'\0' };
3636
const wchar_t notTerminated[3] = { L'a', L'b', L'c' };
37-
// TODO: cppcheck-suppress invalidFunctionArgStr
37+
// cppcheck-suppress invalidFunctionArgStr
3838
(void) wcslen(notTerminated);
3939
(void) wcslen(terminated0);
4040
return wcslen(terminated1);
@@ -3908,10 +3908,11 @@ void bufferAccessOutOfBounds_strxfrm(void)
39083908
{
39093909
const char src[3] = "abc";
39103910
char dest[1] = "a";
3911+
// cppcheck-suppress invalidFunctionArgStr
39113912
(void)strxfrm(dest,src,1);
3912-
// cppcheck-suppress bufferAccessOutOfBounds
3913+
// cppcheck-suppress [bufferAccessOutOfBounds,invalidFunctionArgStr]
39133914
(void)strxfrm(dest,src,2);
3914-
// cppcheck-suppress bufferAccessOutOfBounds
3915+
// cppcheck-suppress [bufferAccessOutOfBounds,invalidFunctionArgStr]
39153916
(void)strxfrm(dest,src,3);
39163917
}
39173918

test/testfunctions.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,18 @@ class TestFunctions : public TestFixture {
711711
" }\n"
712712
"}");
713713
ASSERT_EQUALS("", errout.str());
714+
715+
check("int f() {\n"
716+
" const char c[3] = \"abc\";\n"
717+
" return strlen(c);\n"
718+
"}\n");
719+
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout.str());
720+
721+
check("int f() {\n"
722+
" const wchar_t c[3] = L\"abc\";\n"
723+
" return wcslen(c);\n"
724+
"}\n");
725+
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required.\n", errout.str());
714726
}
715727

716728
void mathfunctionCall_sqrt() {

0 commit comments

Comments
 (0)