Fix #11289 FP wrongPrintfScanfArgNum - snprintf with parameter pack (#4644)

This commit is contained in:
chrchr-github 2022-12-15 08:52:13 +01:00 committed by GitHub
parent b7693ccc7a
commit 0c1e2ceeb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -1306,6 +1306,8 @@ void CheckIO::checkFormatString(const Token * const tok,
// Count printf/scanf parameters.. // Count printf/scanf parameters..
int numFunction = 0; int numFunction = 0;
while (argListTok2) { while (argListTok2) {
if (Token::Match(argListTok2, "%name% ...")) // bailout for parameter pack
return;
numFunction++; numFunction++;
argListTok2 = argListTok2->nextArgument(); // Find next argument argListTok2 = argListTok2->nextArgument(); // Find next argument
} }

View File

@ -80,6 +80,7 @@ private:
TEST_CASE(testPrintfAuto); // #8992 TEST_CASE(testPrintfAuto); // #8992
TEST_CASE(testPrintfParenthesis); // #8489 TEST_CASE(testPrintfParenthesis); // #8489
TEST_CASE(testStdDistance); // #10304 TEST_CASE(testStdDistance); // #10304
TEST_CASE(testParameterPack); // #11289
} }
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
@ -4868,6 +4869,16 @@ private:
"}", /*inconclusive*/ false, /*portability*/ true); "}", /*inconclusive*/ false, /*portability*/ true);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void testParameterPack() { // #11289
check("template <typename... Args> auto f(const char* format, const Args&... args) {\n"
" return snprintf(nullptr, 0, format, args...);\n"
"}\n"
"void g() {\n"
" f(\"%d%d\", 1, 2);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestIO) REGISTER_TEST(TestIO)