Skip to content

Commit c2313ad

Browse files
firewavedanmar
andauthored
fixed #13440 - do not crash when whole program analysis addon execution fails (danmar#7114)
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
1 parent e3dceac commit c2313ad

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

lib/cppcheck.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,8 +1691,15 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
16911691
if (isCtuInfo && addonInfo.name != "misra" && !addonInfo.ctu)
16921692
continue;
16931693

1694-
const std::vector<picojson::value> results =
1695-
executeAddon(addonInfo, mSettings.addonPython, fileList.empty() ? files[0] : fileList, mSettings.premiumArgs, mExecuteCommand);
1694+
std::vector<picojson::value> results;
1695+
1696+
try {
1697+
results = executeAddon(addonInfo, mSettings.addonPython, fileList.empty() ? files[0] : fileList, mSettings.premiumArgs, mExecuteCommand);
1698+
} catch (const InternalError& e) {
1699+
const std::string ctx = isCtuInfo ? "Whole program analysis" : "Checking file";
1700+
const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, file0, "Bailing out from analysis: " + ctx + " failed");
1701+
mErrorLogger.reportErr(errmsg);
1702+
}
16961703

16971704
const bool misraC2023 = mSettings.premiumArgs.find("--misra-c-2023") != std::string::npos;
16981705

@@ -1788,12 +1795,7 @@ void CppCheck::executeAddonsWholeProgram(const std::list<FileWithDetails> &files
17881795
ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName));
17891796
}
17901797

1791-
try {
1792-
executeAddons(ctuInfoFiles, "");
1793-
} catch (const InternalError& e) {
1794-
const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, "", "Bailing out from analysis: Whole program analysis failed");
1795-
mErrorLogger.reportErr(errmsg);
1796-
}
1798+
executeAddons(ctuInfoFiles, "");
17971799
}
17981800

17991801
Settings &CppCheck::settings()

test/cli/other_test.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_progress_j(tmpdir):
197197
assert stderr == ""
198198

199199

200-
def test_execute_addon_failure(tmpdir):
200+
def test_execute_addon_failure_py_auto(tmpdir):
201201
test_file = os.path.join(tmpdir, 'test.cpp')
202202
with open(test_file, 'wt') as f:
203203
f.write("""
@@ -212,21 +212,72 @@ def test_execute_addon_failure(tmpdir):
212212
assert stderr == '{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to auto detect python [internalError]\n\n^\n'.format(test_file)
213213

214214

215-
def test_execute_addon_failure_2(tmpdir):
215+
def test_execute_addon_failure_py_notexist(tmpdir):
216216
test_file = os.path.join(tmpdir, 'test.cpp')
217217
with open(test_file, 'wt') as f:
218218
f.write("""
219219
void f();
220220
""")
221221

222-
# specify non-existent python executbale so execution of addon fails
222+
# specify non-existent python executable so execution of addon fails
223223
args = ['--addon=naming', '--addon-python=python5.x', test_file]
224224

225225
_, _, stderr = cppcheck(args)
226226
ec = 1 if os.name == 'nt' else 127
227227
assert stderr == "{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to execute addon 'naming' - exitcode is {} [internalError]\n\n^\n".format(test_file, ec)
228228

229229

230+
def test_execute_addon_failure_json_notexist(tmpdir):
231+
# specify non-existent python executable so execution of addon fails
232+
addon_json = os.path.join(tmpdir, 'addon.json')
233+
with open(addon_json, 'wt') as f:
234+
f.write(json.dumps({'executable': 'notexist'}))
235+
236+
test_file = os.path.join(tmpdir, 'test.cpp')
237+
with open(test_file, 'wt') as f:
238+
f.write("""
239+
void f();
240+
""")
241+
242+
args = [
243+
'--addon={}'.format(addon_json),
244+
test_file
245+
]
246+
247+
_, _, stderr = cppcheck(args)
248+
ec = 1 if os.name == 'nt' else 127
249+
assert stderr == "{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to execute addon 'addon.json' - exitcode is {} [internalError]\n\n^\n".format(test_file, ec)
250+
251+
252+
def test_execute_addon_failure_json_ctu_notexist(tmpdir):
253+
# specify non-existent python executable so execution of addon fails
254+
addon_json = os.path.join(tmpdir, 'addon.json')
255+
with open(addon_json, 'wt') as f:
256+
f.write(json.dumps({
257+
'executable': 'notexist',
258+
'ctu': True
259+
}))
260+
261+
test_file = os.path.join(tmpdir, 'test.cpp')
262+
with open(test_file, 'wt') as f:
263+
f.write("""
264+
void f(); """)
265+
266+
args = [
267+
'--template=simple',
268+
'--addon={}'.format(addon_json),
269+
test_file
270+
]
271+
272+
_, _, stderr = cppcheck(args)
273+
ec = 1 if os.name == 'nt' else 127
274+
print(stderr)
275+
assert stderr.splitlines() == [
276+
"{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to execute addon 'addon.json' - exitcode is {} [internalError]".format(test_file, ec),
277+
":0:0: error: Bailing out from analysis: Whole program analysis failed: Failed to execute addon 'addon.json' - exitcode is {} [internalError]".format(ec)
278+
]
279+
280+
230281
def test_execute_addon_file0(tmpdir):
231282
test_file = os.path.join(tmpdir, 'test.c')
232283
with open(test_file, 'wt') as f:

0 commit comments

Comments
 (0)