Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/ctu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#include <cstdint>
#include <cstring>
#include <iterator> // back_inserter
#include <set>
#include <sstream>
#include <tuple>
#include <utility>

#include "xml.h"
Expand Down Expand Up @@ -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<std::tuple<std::string, size_t, int>> &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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -584,7 +594,8 @@ std::list<ErrorMessage::FileLocation> 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<std::tuple<std::string, size_t, int>> visited;
if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth, visited))
return {};

if (unknownFunctionReturn && path[0]) {
Expand Down
23 changes: 23 additions & 0 deletions test/cli/performance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ''
Loading