Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Jun 28, 2023
1 parent 3ee90e2 commit 505ac4b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
}

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

Expand Down
5 changes: 2 additions & 3 deletions cli/cmdlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,16 @@ class CmdLineParser {
private:
bool isCppcheckPremium() const;

// TODO: get rid of is_signed
template<typename T>
static bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool is_signed = false)
static bool parseNumberArg(const char* const arg, std::size_t offset, T& num, bool mustBePositive = false)
{
T tmp;
std::string err;
if (!strToInt(arg + offset, tmp, &err)) {
printError("argument to '" + std::string(arg, offset) + "' is not valid - " + err + ".");
return false;
}
if (is_signed && tmp < 0) {
if (mustBePositive && tmp < 0) {
printError("argument to '" + std::string(arg, offset) + "' needs to be a positive integer.");
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ class TestCmdlineParser : public TestFixture {
REDIRECT;
const char * const argv[] = {"cppcheck", "--checks-max-time=-1", "file.cpp"};
ASSERT(!defParser.parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: argument to '--checks-max-time=' is not valid - needs to be positive.\n", GET_REDIRECT_OUTPUT);
ASSERT_EQUALS("cppcheck: error: argument to '--checks-max-time=' needs to be a positive integer.\n", GET_REDIRECT_OUTPUT);
}

void checksMaxTimeInvalid() {
Expand Down

0 comments on commit 505ac4b

Please sign in to comment.