From 55af68aaf76788be09f0f6888f7bff526c72db86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 28 Jun 2023 19:52:33 +0200 Subject: [PATCH] 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). --- cli/cmdlineparser.cpp | 2 +- cli/cmdlineparser.h | 5 ++--- lib/settings.h | 2 +- test/testcmdlineparser.cpp | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index acd5a642d..d76edbf91 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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; } diff --git a/cli/cmdlineparser.h b/cli/cmdlineparser.h index efcdb3f5d..d12a5247b 100644 --- a/cli/cmdlineparser.h +++ b/cli/cmdlineparser.h @@ -123,9 +123,8 @@ protected: private: bool isCppcheckPremium() const; - // TODO: get rid of is_signed template - 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; @@ -133,7 +132,7 @@ private: 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; } diff --git a/lib/settings.h b/lib/settings.h index eb3d4e18b..c77ad2392 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -128,7 +128,7 @@ public: bool checkLibrary; /** @brief The maximum time in seconds for the checks of a single file */ - std::size_t checksMaxTime; + int checksMaxTime; /** @brief check unknown function return values */ std::set checkUnknownFunctionReturn; diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 69e6026ca..fffa19f5f 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -1684,7 +1684,7 @@ private: void checksMaxTime() { REDIRECT; const char * const argv[] = {"cppcheck", "--checks-max-time=12", "file.cpp"}; - settings.checksMaxTime = SIZE_MAX; + settings.checksMaxTime = 0; ASSERT(defParser.parseFromArgs(3, argv)); ASSERT_EQUALS(12, settings.checksMaxTime); ASSERT_EQUALS("", GET_REDIRECT_OUTPUT); @@ -1694,7 +1694,7 @@ private: 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() {