Skip to content

Commit ae6a77d

Browse files
authored
Fix #14798: internalError on sscanf format string (#8724)
1 parent a0a31d9 commit ae6a77d

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/checkio.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ void CheckIOImpl::checkFormatString(const Token * const tok,
841841
}
842842
++i;
843843
}
844+
while (!width.empty() && width[0] == '0')
845+
width = width.substr(1);
844846
auto bracketBeg = formatString.cend();
845847
if (i != formatString.cend() && *i == '[') {
846848
bracketBeg = i;

test/testio.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TestIO : public TestFixture {
5353
TEST_CASE(testScanf3); // #3494
5454
TEST_CASE(testScanf4); // #ticket 2553
5555
TEST_CASE(testScanf5); // #10632
56+
TEST_CASE(testScanf6);
5657

5758
mNewTemplate = false;
5859
TEST_CASE(testScanfArgument);
@@ -892,6 +893,14 @@ class TestIO : public TestFixture {
892893
"[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());
893894
}
894895

896+
void testScanf6() {
897+
ASSERT_NO_THROW(check("int f(const char *p) {\n"
898+
" char a[3];\n"
899+
" return sscanf(p, \"%02s\", a);\n"
900+
"}\n"));
901+
ASSERT_EQUALS("", errout_str());
902+
}
903+
895904

896905
#define TEST_SCANF_CODE(format, type) \
897906
"void f(){" type " x; scanf(\"" format "\", &x);}"

0 commit comments

Comments
 (0)