Skip to content

Commit 9d2720a

Browse files
authored
Fix #15180 internalError in checkFormatString on invalid width or position (#8982)
CheckIOImpl::checkFormatString converts the digits collected for a field width or a POSIX parameter position with strToInt<int>, which throws when the digits are empty (`%$d`), start with a zero (`%01$d`, which glibc accepts) or do not fit in an int (`%99999999999$d`, `scanf("%99999999999s", buf)` with a known array), so one such format string makes CppCheck::checkFile bail out with an internalError and drop every other diagnostic for that file. Replaced the conversion with a small helper that parses the digits without throwing and clamps anything above INT_MAX, and a `$` with no digits in front is no longer taken as a parameter position; widths and positions that fit in an int behave exactly as before, while an oversized one is now reported through the existing invalidScanfFormatWidth and wrongPrintfScanfParameterPositionError messages, with 2147483647 shown for the clamped value. Verified with an ASan/UBSan build: the new asserts in TestIO::testScanf4 and testPosixPrintfScanfParameterPosition throw on main and pass with the fix, the full testrunner is green, and the selfcheck options report nothing for lib/checkio.cpp. I do not have Trac access, so could you create a ticket for this? I will update the title to reference it once there is a number.
1 parent 68bf94a commit 9d2720a

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎lib/checkio.cpp‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <cctype>
3636
#include <cstdlib>
3737
#include <functional>
38+
#include <limits>
3839
#include <list>
3940
#include <map>
4041
#include <set>
@@ -794,6 +795,20 @@ void CheckIOImpl::checkWrongPrintfScanfArguments()
794795
}
795796
}
796797

798+
// Convert the digits of a format string width or parameter position.
799+
// A value that does not fit in an int is clamped to INT_MAX so a huge
800+
// width or position is still reported instead of aborting the check.
801+
static int formatStringNumber(const std::string& digits)
802+
{
803+
MathLib::bigint value = 0;
804+
for (const char c : digits) {
805+
value = (value * 10) + (c - '0');
806+
if (value > std::numeric_limits<int>::max())
807+
return std::numeric_limits<int>::max();
808+
}
809+
return static_cast<int>(value);
810+
}
811+
797812
void CheckIOImpl::checkFormatString(const Token * const tok,
798813
const Token * const formatStringTok,
799814
const Token * argListTok,
@@ -853,8 +868,10 @@ void CheckIOImpl::checkFormatString(const Token * const tok,
853868
} else if (std::isdigit(*i)) {
854869
width += *i;
855870
} else if (*i == '$') {
856-
parameterPosition = strToInt<int>(width);
857-
hasParameterPosition = true;
871+
if (!width.empty()) {
872+
parameterPosition = formatStringNumber(width);
873+
hasParameterPosition = true;
874+
}
858875
width.clear();
859876
}
860877
++i;
@@ -906,7 +923,7 @@ void CheckIOImpl::checkFormatString(const Token * const tok,
906923
specifier += (*i == 's' || bracketBeg == formatString.end()) ? std::string{ 's' } : std::string{ bracketBeg, i + 1 };
907924
if (argInfo.variableInfo && argInfo.isKnownType() && argInfo.variableInfo->isArray() && (argInfo.variableInfo->dimensions().size() == 1) && argInfo.variableInfo->dimensions()[0].known) {
908925
if (!width.empty()) {
909-
const int numWidth = strToInt<int>(width);
926+
const int numWidth = formatStringNumber(width);
910927
if (numWidth != (argInfo.variableInfo->dimension(0) - 1))
911928
invalidScanfFormatWidthError(tok, numFormat, numWidth, argInfo.variableInfo, specifier);
912929
}
@@ -929,7 +946,7 @@ void CheckIOImpl::checkFormatString(const Token * const tok,
929946
case 'c':
930947
if (argInfo.variableInfo && argInfo.isKnownType() && argInfo.variableInfo->isArray() && (argInfo.variableInfo->dimensions().size() == 1) && argInfo.variableInfo->dimensions()[0].known) {
931948
if (!width.empty()) {
932-
const int numWidth = strToInt<int>(width);
949+
const int numWidth = formatStringNumber(width);
933950
if (numWidth > argInfo.variableInfo->dimension(0))
934951
invalidScanfFormatWidthError(tok, numFormat, numWidth, argInfo.variableInfo, std::string(1, *i));
935952
}

‎test/testio.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,14 @@ class TestIO : public TestFixture {
899899
" scanf (\"%70s\",str);\n"
900900
"}\n");
901901
ASSERT_EQUALS("[test.cpp:4:3]: (error) Width 70 given in format string (no. 1) is larger than destination buffer 'str[8]', use %7s to prevent overflowing it. [invalidScanfFormatWidth]\n", errout_str());
902+
903+
// width that does not fit in an int
904+
check("void f()\n"
905+
"{\n"
906+
" char str [8];\n"
907+
" scanf (\"%99999999999s\",str);\n"
908+
"}\n");
909+
ASSERT_EQUALS("[test.cpp:4:3]: (error) Width 2147483647 given in format string (no. 1) is larger than destination buffer 'str[8]', use %7s to prevent overflowing it. [invalidScanfFormatWidth]\n", errout_str());
902910
}
903911

904912
void testScanf5() { // #10632
@@ -4274,6 +4282,14 @@ class TestIO : public TestFixture {
42744282
"[test.cpp:5:3]: (warning) scanf: referencing parameter 2 while 1 arguments given [wrongPrintfScanfParameterPositionError]\n"
42754283
"[test.cpp:6:3]: (warning) printf: parameter positions start at 1, not 0 [wrongPrintfScanfParameterPositionError]\n"
42764284
"", errout_str());
4285+
4286+
// parameter position without digits, with a leading zero or too large for an int
4287+
check("void foo() {\n"
4288+
" printf(\"%$d\", 1);\n"
4289+
" printf(\"%01$d\", 1);\n"
4290+
" printf(\"%99999999999$d\", 1);\n"
4291+
"}\n");
4292+
ASSERT_EQUALS("[test.cpp:4:3]: (warning) printf: referencing parameter 2147483647 while 1 arguments given [wrongPrintfScanfParameterPositionError]\n", errout_str());
42774293
}
42784294

42794295

0 commit comments

Comments
 (0)