diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 3d82bdb796a..468e2867619 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -841,6 +841,8 @@ void CheckIOImpl::checkFormatString(const Token * const tok, } ++i; } + while (!width.empty() && width[0] == '0') + width = width.substr(1); auto bracketBeg = formatString.cend(); if (i != formatString.cend() && *i == '[') { bracketBeg = i; diff --git a/test/testio.cpp b/test/testio.cpp index f90773d4cf4..843c5a62ccc 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -53,6 +53,7 @@ class TestIO : public TestFixture { TEST_CASE(testScanf3); // #3494 TEST_CASE(testScanf4); // #ticket 2553 TEST_CASE(testScanf5); // #10632 + TEST_CASE(testScanf6); mNewTemplate = false; TEST_CASE(testScanfArgument); @@ -892,6 +893,14 @@ class TestIO : public TestFixture { "[test.cpp:3:5]: (error) Width 42 given in format string (no. 2) is larger than destination buffer 's2[42]', use %41[a-z] to prevent overflowing it. [invalidScanfFormatWidth]\n", errout_str()); } + void testScanf6() { + ASSERT_NO_THROW(check("int f(const char *p) {\n" + " char a[3];\n" + " return sscanf(p, \"%02s\", a);\n" + "}\n")); + ASSERT_EQUALS("", errout_str()); + } + #define TEST_SCANF_CODE(format, type) \ "void f(){" type " x; scanf(\"" format "\", &x);}"