Skip to content

Commit 55af68a

Browse files
authored
Update type for Settings::checksMaxTime. (#5205)
It's a time offset not a size. It should not have value SIZE_MAX that makes it ineffective (overflow in calculation of stop time).
1 parent 767c0fb commit 55af68a

4 files changed

Lines changed: 6 additions & 7 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
263263
}
264264

265265
else if (std::strncmp(argv[i], "--checks-max-time=", 18) == 0) {
266-
if (!parseNumberArg(argv[i], 18, mSettings.checksMaxTime))
266+
if (!parseNumberArg(argv[i], 18, mSettings.checksMaxTime, true))
267267
return false;
268268
}
269269

cli/cmdlineparser.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,16 @@ class CmdLineParser {
123123
private:
124124
bool isCppcheckPremium() const;
125125

126-
// TODO: get rid of is_signed
127126
template<typename T>
128-
static bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool is_signed = false)
127+
static bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
129128
{
130129
T tmp;
131130
std::string err;
132131
if (!strToInt(arg + offset, tmp, &err)) {
133132
printError("argument to '" + std::string(arg, offset) + "' is not valid - " + err + ".");
134133
return false;
135134
}
136-
if (is_signed && tmp < 0) {
135+
if (mustBePositive && tmp < 0) {
137136
printError("argument to '" + std::string(arg, offset) + "' needs to be a positive integer.");
138137
return false;
139138
}

lib/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class CPPCHECKLIB Settings {
128128
bool checkLibrary;
129129

130130
/** @brief The maximum time in seconds for the checks of a single file */
131-
std::size_t checksMaxTime;
131+
int checksMaxTime;
132132

133133
/** @brief check unknown function return values */
134134
std::set<std::string> checkUnknownFunctionReturn;

test/testcmdlineparser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ class TestCmdlineParser : public TestFixture {
16841684
void checksMaxTime() {
16851685
REDIRECT;
16861686
const char * const argv[] = {"cppcheck", "--checks-max-time=12", "file.cpp"};
1687-
settings.checksMaxTime = SIZE_MAX;
1687+
settings.checksMaxTime = 0;
16881688
ASSERT(defParser.parseFromArgs(3, argv));
16891689
ASSERT_EQUALS(12, settings.checksMaxTime);
16901690
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
@@ -1694,7 +1694,7 @@ class TestCmdlineParser : public TestFixture {
16941694
REDIRECT;
16951695
const char * const argv[] = {"cppcheck", "--checks-max-time=-1", "file.cpp"};
16961696
ASSERT(!defParser.parseFromArgs(3, argv));
1697-
ASSERT_EQUALS("cppcheck: error: argument to '--checks-max-time=' is not valid - needs to be positive.\n", GET_REDIRECT_OUTPUT);
1697+
ASSERT_EQUALS("cppcheck: error: argument to '--checks-max-time=' needs to be a positive integer.\n", GET_REDIRECT_OUTPUT);
16981698
}
16991699

17001700
void checksMaxTimeInvalid() {

0 commit comments

Comments
 (0)