Fixed FP, found with daca@home: ftp://ftp.de.debian.org/debian/pool/main/k/knot/knot_3.1.8.orig.tar.xz; knot-3.1.8/tests/knot/test_confio.c:301:18: error: Invalid strcmp() argument nr 2. A nul-terminated string is required. [invalidFunctionArgStr]

This commit is contained in:
orbitcowboy 2022-06-03 13:10:45 +02:00
parent 7dac213734
commit 1a4bd0a7b3
2 changed files with 29 additions and 7 deletions

View File

@ -125,7 +125,7 @@ void CheckFunctions::invalidFunctionUsage()
else if (!mSettings->library.isIntArgValid(functionToken, argnr, 1)) else if (!mSettings->library.isIntArgValid(functionToken, argnr, 1))
invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, mSettings->library.validarg(functionToken, argnr)); invalidFunctionArgError(argtok, functionToken->str(), argnr, nullptr, mSettings->library.validarg(functionToken, argnr));
} }
// check <strz> // check <strz>
if (mSettings->library.isargstrz(functionToken, argnr)) { if (mSettings->library.isargstrz(functionToken, argnr)) {
if (Token::Match(argtok, "& %var% !![") && argtok->next() && argtok->next()->valueType()) { if (Token::Match(argtok, "& %var% !![") && argtok->next() && argtok->next()->valueType()) {
const ValueType * valueType = argtok->next()->valueType(); const ValueType * valueType = argtok->next()->valueType();
@ -151,13 +151,17 @@ void CheckFunctions::invalidFunctionUsage()
if (Token::simpleMatch(varTok, "= {")) { if (Token::simpleMatch(varTok, "= {")) {
varTok = varTok->tokAt(1); varTok = varTok->tokAt(1);
auto charsUntilFirstZero = 0; auto charsUntilFirstZero = 0;
while (varTok && !Token::simpleMatch(varTok->next(), "}")) { bool search = true;
if (!Token::simpleMatch(varTok->next(), ",")) { while (search && varTok && !Token::simpleMatch(varTok->next(), "}")) {
++charsUntilFirstZero;
}
varTok = varTok->next(); varTok = varTok->next();
if (varTok && varTok->hasKnownIntValue() && varTok->getKnownIntValue() == 0) { if (!Token::simpleMatch(varTok, ",")) {
break; // stop counting for cases like char buf[3] = {'x', '\0', 'y'}; if (Token::Match(varTok, "%op%")) {
varTok = varTok->next();
continue;
}
++charsUntilFirstZero;
if (varTok && varTok->hasKnownIntValue() && varTok->getKnownIntValue() == 0)
search=false; // stop counting for cases like char buf[3] = {'x', '\0', 'y'};
} }
} }
if (varTok && varTok->hasKnownIntValue() && varTok->getKnownIntValue() != 0 if (varTok && varTok->hasKnownIntValue() && varTok->getKnownIntValue() != 0

View File

@ -650,6 +650,24 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("FILE* f(void) {\n"
" const char fileName[] = { \'0\' + 42 };\n" // no size is explicitly defined, no implicit '\0' is added
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout.str());
check("FILE* f(void) {\n"
" const char fileName[] = { \'0\' + 42, \'x\' };\n" // no size is explicitly defined, no implicit '\0' is added
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout.str());
check("FILE* f(void) {\n"
" const char fileName[2] = { \'0\' + 42 };\n" // implicitly '\0' added at the end because size is set to 2
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout.str());
check("FILE* f(void) {\n" check("FILE* f(void) {\n"
" const char fileName[] = { };\n" " const char fileName[] = { };\n"
" return fopen(fileName, \"r\"); \n" " return fopen(fileName, \"r\"); \n"