Skip to content

Commit bb3c7f8

Browse files
committed
Fix severity, use index, fix config, restore warning
1 parent 062dd28 commit bb3c7f8

6 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎cfg/posix.cfg‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,11 +2148,13 @@ The function 'mktemp' is considered to be dangerous due to race conditions and s
21482148
<arg nr="1" direction="in">
21492149
<not-null/>
21502150
<not-uninit/>
2151+
<strz/>
21512152
<minsize type="argvalue" arg="3"/>
21522153
</arg>
21532154
<arg nr="2" direction="in">
21542155
<not-null/>
21552156
<not-uninit/>
2157+
<strz/>
21562158
<minsize type="argvalue" arg="3"/>
21572159
</arg>
21582160
<arg nr="3" direction="in">

‎cfg/windows.cfg‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,11 +4319,13 @@ HFONT CreateFont(
43194319
<arg nr="1" direction="in">
43204320
<not-null/>
43214321
<not-uninit/>
4322+
<strz/>
43224323
<minsize type="argvalue" arg="3"/>
43234324
</arg>
43244325
<arg nr="2" direction="in">
43254326
<not-null/>
43264327
<not-uninit/>
4328+
<strz/>
43274329
<minsize type="argvalue" arg="3"/>
43284330
</arg>
43294331
<arg nr="3" direction="in">

‎lib/checkbufferoverrun.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,11 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
606606
}
607607
} else if (checkStringLiteral && bufTok->tokType() == Token::Type::eString && bufTok->valueType()) {
608608
ValueFlow::Value strSizeVal;
609+
strSizeVal.setKnown();
609610
strSizeVal.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;
610611
const size_t charSize = bufTok->valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
611612
if (charSize > 0) {
612-
strSizeVal.intvalue = Token::getStrArraySize(bufTok) * charSize;
613+
strSizeVal.intvalue = (Token::getStrArraySize(bufTok) - index) * charSize;
613614
return strSizeVal;
614615
}
615616
}

‎lib/checkfunctions.cpp‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ void CheckFunctionsImpl::functionCalledError(const Token* tok, Severity severity
102102
reportError(tok, severity, prefix + "Called", msg, CWE477, Certainty::normal);
103103
}
104104

105-
static bool hasArgMinSize(const Token* ftok, int argnr, const Settings& settings)
105+
static bool hasArgMinReadSize(const Token *ftok, int argnr, const Library &library)
106106
{
107-
if (const std::vector<Library::ArgumentChecks::MinSize>* minsizes = settings.library.argminsizes(ftok, argnr)) {
108-
return std::any_of(minsizes->begin(), minsizes->end(), [](const Library::ArgumentChecks::MinSize& ms) {
109-
return ms.type == Library::ArgumentChecks::MinSize::Type::ARGVALUE;
110-
});
107+
if (library.getArgDirection(ftok, argnr) == Library::ArgumentChecks::Direction::DIR_IN) {
108+
if (const std::vector<Library::ArgumentChecks::MinSize> *minsizes = library.argminsizes(ftok, argnr)) {
109+
return std::any_of(minsizes->begin(), minsizes->end(), [](const Library::ArgumentChecks::MinSize &ms) {
110+
return ms.type == Library::ArgumentChecks::MinSize::Type::ARGVALUE;
111+
});
112+
}
111113
}
112114
return false;
113115
}
@@ -147,7 +149,7 @@ void CheckFunctionsImpl::invalidFunctionUsage()
147149
invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, mSettings.library.validarg(functionToken, argnr));
148150
}
149151
// check <strz>
150-
if (mSettings.library.isargstrz(functionToken, argnr) && !hasArgMinSize(functionToken, argnr, mSettings)) {
152+
if (mSettings.library.isargstrz(functionToken, argnr) && !hasArgMinReadSize(functionToken, argnr, mSettings.library)) {
151153
if (Token::Match(argtok, "& %var% !![") && argtok->next() && argtok->next()->valueType()) {
152154
const ValueType * valueType = argtok->next()->valueType();
153155
const Variable * variable = argtok->next()->variable();

‎test/cfg/std.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void invalidFunctionArgStr_strncat(void)
7878
// No warning is expected for
7979
strncat (str1, &src, 1);
8080
puts (str1);
81+
char d[] = {'a','b','c'};
82+
// cppcheck-suppress invalidFunctionArgStr
83+
strncat(d, "def", 1);
8184
}
8285

8386
char * invalidFunctionArgStr_strpbrk( const char *p )

‎test/testbufferoverrun.cpp‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3650,7 +3650,7 @@ class TestBufferOverrun : public TestFixture {
36503650
" char buf[128];\n"
36513651
" memcpy(buf, \"abc\", 5);\n"
36523652
"}\n");
3653-
ASSERT_EQUALS("[test.cpp:3:17]: (warning) Buffer is accessed out of bounds: \"abc\" [bufferAccessOutOfBounds]\n",
3653+
ASSERT_EQUALS("[test.cpp:3:17]: (error) Buffer is accessed out of bounds: \"abc\" [bufferAccessOutOfBounds]\n",
36543654
errout_str());
36553655

36563656
check("void f() {\n"
@@ -3663,7 +3663,7 @@ class TestBufferOverrun : public TestFixture {
36633663
" char buf[128];\n"
36643664
" memcpy(buf, L\"abc\", 4 * sizeof(wchar_t) + 1);\n"
36653665
"}\n");
3666-
ASSERT_EQUALS("[test.cpp:3:17]: (warning) Buffer is accessed out of bounds: L\"abc\" [bufferAccessOutOfBounds]\n",
3666+
ASSERT_EQUALS("[test.cpp:3:17]: (error) Buffer is accessed out of bounds: L\"abc\" [bufferAccessOutOfBounds]\n",
36673667
errout_str());
36683668

36693669
check("int f(char* s) {\n"
@@ -3675,6 +3675,12 @@ class TestBufferOverrun : public TestFixture {
36753675
" return memcmp(buf, u\"abc\", 4 * sizeof(char16_t));\n"
36763676
"}\n");
36773677
ASSERT_EQUALS("", errout_str());
3678+
3679+
check("int f(const char* buf) {\n"
3680+
" return memcmp(buf, &\"abc\"[2], 4 * sizeof(char));\n"
3681+
"}\n");
3682+
ASSERT_EQUALS("[test.cpp:2:24]: (error) Buffer is accessed out of bounds: &\"abc\"[2] [bufferAccessOutOfBounds]\n",
3683+
errout_str());
36783684
}
36793685

36803686
void buffer_overrun_errorpath() {
@@ -4657,7 +4663,7 @@ class TestBufferOverrun : public TestFixture {
46574663
check("void f() {\n"
46584664
" mymemset(\"abc\", 0, 20);\n"
46594665
"}\n", settings);
4660-
ASSERT_EQUALS("[test.cpp:2:12]: (warning) Buffer is accessed out of bounds: \"abc\" [bufferAccessOutOfBounds]\n",
4666+
ASSERT_EQUALS("[test.cpp:2:12]: (error) Buffer is accessed out of bounds: \"abc\" [bufferAccessOutOfBounds]\n",
46614667
errout_str());
46624668

46634669
check("void f() {\n"

0 commit comments

Comments
 (0)