Skip to content

Commit 1b0ca08

Browse files
committed
Bug hunting; option to set function analysis max time
1 parent 87468df commit 1b0ca08

5 files changed

Lines changed: 22 additions & 0 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
219219
else if (std::strcmp(argv[i], "--bug-hunting") == 0)
220220
mSettings->bugHunting = true;
221221

222+
// TODO: Rename or move this parameter?
223+
else if (std::strncmp(argv[i], "--bug-hunting-check-function-max-time=", 38) == 0)
224+
mSettings->bugHuntingCheckFunctionMaxTime = std::atoi(argv[i] + 38);
225+
222226
// Check configuration
223227
else if (std::strcmp(argv[i], "--check-config") == 0)
224228
mSettings->checkConfiguration = true;

lib/exprengine.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,10 @@ static std::string execute(const Token *start, const Token *end, Data &data)
24282428
};
24292429
Recursion updateRecursion(&data.recursion, data.recursion);
24302430

2431+
const std::time_t stopTime = (data.settings->bugHuntingCheckFunctionMaxTime > 0) ?
2432+
(data.startTime + data.settings->bugHuntingCheckFunctionMaxTime) :
2433+
~0ULL;
2434+
24312435
for (const Token *tok = start; tok != end; tok = tok->next()) {
24322436
if (Token::Match(tok, "[;{}]")) {
24332437
data.trackProgramState(tok);
@@ -2438,6 +2442,8 @@ static std::string execute(const Token *start, const Token *end, Data &data)
24382442
if (Token::Match(prev, "[;{}] return|throw"))
24392443
return data.str();
24402444
}
2445+
if (std::time(nullptr) > stopTime)
2446+
return "";
24412447
}
24422448

24432449
if (Token::simpleMatch(tok, "__CPPCHECK_BAILOUT__ ;"))
@@ -2850,6 +2856,10 @@ void ExprEngine::executeFunction(const Scope *functionScope, ErrorLogger *errorL
28502856

28512857
data.contractConstraints(function, executeExpression1);
28522858

2859+
const std::time_t stopTime = (data.settings->bugHuntingCheckFunctionMaxTime > 0) ?
2860+
(data.startTime + data.settings->bugHuntingCheckFunctionMaxTime) :
2861+
~0ULL;
2862+
28532863
try {
28542864
execute(functionScope->bodyStart, functionScope->bodyEnd, data);
28552865
} catch (const ExprEngineException &e) {
@@ -2858,6 +2868,8 @@ void ExprEngine::executeFunction(const Scope *functionScope, ErrorLogger *errorL
28582868
trackExecution.setAbortLine(e.tok->linenr());
28592869
auto bailoutValue = std::make_shared<BailoutValue>();
28602870
for (const Token *tok = e.tok; tok != functionScope->bodyEnd; tok = tok->next()) {
2871+
if (std::time(nullptr) >= stopTime)
2872+
break;
28612873
if (Token::Match(tok, "return|throw|while|if|for (")) {
28622874
tok = tok->next();
28632875
continue;

lib/settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const char Settings::SafeChecks::XmlExternalVariables[] = "external-variables";
3131
Settings::Settings()
3232
: mEnabled(0),
3333
bugHunting(false),
34+
bugHuntingCheckFunctionMaxTime(0),
3435
checkAllConfigurations(true),
3536
checkConfiguration(false),
3637
checkHeaders(true),

lib/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class CPPCHECKLIB Settings : public cppcheck::Platform {
8383
/** @brief Bug hunting */
8484
bool bugHunting;
8585

86+
/** @brief Max time for bug hunting analysis in seconds, after
87+
* timeout the analysis will just stop. */
88+
int bugHuntingCheckFunctionMaxTime;
89+
8690
/** Filename for bug hunting report */
8791
std::string bugHuntingReport;
8892

test/bug-hunting/itc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def get_error_lines(filename):
4444
def check(filename):
4545
cmd = [CPPCHECK_PATH,
4646
'--bug-hunting',
47+
'--bug-hunting-check-function-max-time=10'
4748
'--platform=unix64',
4849
filename]
4950
if RUN_CLANG:

0 commit comments

Comments
 (0)