From 0c1e2ceeb936ad4efdf5199fa5b42672bcbb8338 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 15 Dec 2022 08:52:13 +0100 Subject: [PATCH] Fix #11289 FP wrongPrintfScanfArgNum - snprintf with parameter pack (#4644) --- lib/checkio.cpp | 2 ++ test/testio.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index fa896a7d3..8b0c49974 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1306,6 +1306,8 @@ void CheckIO::checkFormatString(const Token * const tok, // Count printf/scanf parameters.. int numFunction = 0; while (argListTok2) { + if (Token::Match(argListTok2, "%name% ...")) // bailout for parameter pack + return; numFunction++; argListTok2 = argListTok2->nextArgument(); // Find next argument } diff --git a/test/testio.cpp b/test/testio.cpp index ab200834a..91280a0fe 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -80,6 +80,7 @@ private: TEST_CASE(testPrintfAuto); // #8992 TEST_CASE(testPrintfParenthesis); // #8489 TEST_CASE(testStdDistance); // #10304 + TEST_CASE(testParameterPack); // #11289 } #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) @@ -4868,6 +4869,16 @@ private: "}", /*inconclusive*/ false, /*portability*/ true); ASSERT_EQUALS("", errout.str()); } + + void testParameterPack() { // #11289 + check("template 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)