diff --git a/lib/ctu.cpp b/lib/ctu.cpp index 3346329dc65..498d501cfb3 100644 --- a/lib/ctu.cpp +++ b/lib/ctu.cpp @@ -32,7 +32,9 @@ #include #include #include // back_inserter +#include #include +#include #include #include "xml.h" @@ -507,11 +509,19 @@ static bool findPath(const std::string &callId, const CTU::FileInfo::CallBase *path[10], int index, bool warning, - int maxCtuDepth) + int maxCtuDepth, + std::set> &visited) { if (index >= maxCtuDepth) return false; // TODO: add bailout message? + // Within one search the result only depends on callId, callArgNr and index. + // A repeated state has failed before (a success ends the whole search). + // Without this the search is exponential when the same calls are reachable + // on many paths, for example calls in a header that many files include. + if (!visited.emplace(callId, callArgNr, index).second) + return false; + const auto it = callsMap.find(callId); if (it == callsMap.end()) return false; @@ -550,7 +560,7 @@ static bool findPath(const std::string &callId, if (!nestedCall) continue; - if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) { + if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth, visited)) { path[index] = nestedCall; return true; } @@ -584,7 +594,8 @@ std::list CTU::FileInfo::getErrorPath(InvalidValueTy { const CTU::FileInfo::CallBase *path[10] = {nullptr}; - if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth)) + std::set> visited; + if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth, visited)) return {}; if (unknownFunctionReturn && path[0]) { diff --git a/test/cli/performance_test.py b/test/cli/performance_test.py index ea1ad8bd5ff..e45f43a8cdf 100644 --- a/test/cli/performance_test.py +++ b/test/cli/performance_test.py @@ -478,3 +478,26 @@ def test_large_number_of_violations_and_suppressions(tmpdir): f.write(f'misra-c2012-21.1:**/{i}/{header_name}\n') cppcheck([filename_main] + ['--addon=misra.py', '--check-level=exhaustive', '--enable=all', f'--suppressions-list={suppressions_file}']) + + +@pytest.mark.timeout(20) +def test_slow_ctu_header_included_by_many_files(tmpdir): + # 15292: whole program analysis was very slow when a header with nested calls is included by many files + with open(os.path.join(tmpdir, 'header.h'), 'wt') as f: + f.write("inline int f0(const char *s, int i) { return s[i] - '0'; }\n") + for level in range(1, 5): + 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') + files = [] + for i in range(100): + filename = f'test{i}.cpp' + with open(os.path.join(tmpdir, filename), 'wt') as f: + f.write('#include "header.h"\n' + f'int g{i}(void) {{ return f4("0123456789abcdef", 0); }}\n') + files.append(filename) + build_dir = os.path.join(tmpdir, 'b1') + os.mkdir(build_dir) + args = ['-q', '-j2', '--max-ctu-depth=4', f'--cppcheck-build-dir={build_dir}'] + files + ret, stdout, stderr = cppcheck(args, cwd=tmpdir) # should not take more than ~1 second + assert ret == 0, stdout + assert stdout == '' + assert stderr == ''