Skip to content

Commit b03a347

Browse files
authored
Fix #15292 Whole program analysis hangs with --cppcheck-build-dir when a header with nested calls is included by many files (#9093)
1 parent 5e00e72 commit b03a347

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎lib/ctu.cpp‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include <cstdint>
3333
#include <cstring>
3434
#include <iterator> // back_inserter
35+
#include <set>
3536
#include <sstream>
37+
#include <tuple>
3638
#include <utility>
3739

3840
#include "xml.h"
@@ -507,11 +509,19 @@ static bool findPath(const std::string &callId,
507509
const CTU::FileInfo::CallBase *path[10],
508510
int index,
509511
bool warning,
510-
int maxCtuDepth)
512+
int maxCtuDepth,
513+
std::set<std::tuple<std::string, size_t, int>> &visited)
511514
{
512515
if (index >= maxCtuDepth)
513516
return false; // TODO: add bailout message?
514517

518+
// Within one search the result only depends on callId, callArgNr and index.
519+
// A repeated state has failed before (a success ends the whole search).
520+
// Without this the search is exponential when the same calls are reachable
521+
// on many paths, for example calls in a header that many files include.
522+
if (!visited.emplace(callId, callArgNr, index).second)
523+
return false;
524+
515525
const auto it = callsMap.find(callId);
516526
if (it == callsMap.end())
517527
return false;
@@ -550,7 +560,7 @@ static bool findPath(const std::string &callId,
550560
if (!nestedCall)
551561
continue;
552562

553-
if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) {
563+
if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth, visited)) {
554564
path[index] = nestedCall;
555565
return true;
556566
}
@@ -584,7 +594,8 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
584594
{
585595
const CTU::FileInfo::CallBase *path[10] = {nullptr};
586596

587-
if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth))
597+
std::set<std::tuple<std::string, size_t, int>> visited;
598+
if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth, visited))
588599
return {};
589600

590601
if (unknownFunctionReturn && path[0]) {

‎test/cli/performance_test.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,26 @@ def test_large_number_of_violations_and_suppressions(tmpdir):
478478
f.write(f'misra-c2012-21.1:**/{i}/{header_name}\n')
479479

480480
cppcheck([filename_main] + ['--addon=misra.py', '--check-level=exhaustive', '--enable=all', f'--suppressions-list={suppressions_file}'])
481+
482+
483+
@pytest.mark.timeout(20)
484+
def test_slow_ctu_header_included_by_many_files(tmpdir):
485+
# 15292: whole program analysis was very slow when a header with nested calls is included by many files
486+
with open(os.path.join(tmpdir, 'header.h'), 'wt') as f:
487+
f.write("inline int f0(const char *s, int i) { return s[i] - '0'; }\n")
488+
for level in range(1, 5):
489+
f.write(f'inline int f{level}(const char *s, int i) {{ return f{level - 1}(s, i) + f{level - 1}(s, i + 1); }}\n')
490+
files = []
491+
for i in range(100):
492+
filename = f'test{i}.cpp'
493+
with open(os.path.join(tmpdir, filename), 'wt') as f:
494+
f.write('#include "header.h"\n'
495+
f'int g{i}(void) {{ return f4("0123456789abcdef", 0); }}\n')
496+
files.append(filename)
497+
build_dir = os.path.join(tmpdir, 'b1')
498+
os.mkdir(build_dir)
499+
args = ['-q', '-j2', '--max-ctu-depth=4', f'--cppcheck-build-dir={build_dir}'] + files
500+
ret, stdout, stderr = cppcheck(args, cwd=tmpdir) # should not take more than ~1 second
501+
assert ret == 0, stdout
502+
assert stdout == ''
503+
assert stderr == ''

0 commit comments

Comments
 (0)