Skip to content

Commit 6d7e1da

Browse files
authored
Fix #14936: Failures to add suppressions are ignored in importCppcheckGuiProject (#8757)
1 parent 422e90c commit 6d7e1da

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

lib/importproject.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,18 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings &setti
16531653

16541654
for (const std::string &p : paths)
16551655
guiProject.pathNames.push_back(Path::fromNativeSeparators(p));
1656-
supprs.nomsg.addSuppressions(std::move(suppressions)); // TODO: check result
1656+
1657+
bool ok = true;
1658+
for (const auto &suppression : suppressions) {
1659+
const std::string addError = supprs.nomsg.addSuppression(suppression);
1660+
if (!addError.empty()) {
1661+
errors.emplace_back(addError);
1662+
ok = false;
1663+
}
1664+
}
1665+
if (!ok)
1666+
return false;
1667+
16571668
settings.checkHeaders = temp.checkHeaders;
16581669
settings.checkUnusedTemplates = temp.checkUnusedTemplates;
16591670
settings.maxCtuDepth = temp.maxCtuDepth;

test/cli/more-projects_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def test_project_empty_fields(tmpdir):
173173
</libraries>
174174
<suppressions/>
175175
<suppressions>
176-
<suppression/>
177176
</suppressions>
178177
<vs-configurations/>
179178
<vs-configurations>

test/cli/project-suppressions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# python -m pytest project-suppressions.py
3+
4+
from testutils import create_gui_project_file, assert_cppcheck
5+
6+
def test_cli_and_project_suppressions(tmp_path):
7+
# Uninitvar suppressed in project file
8+
suppressions = [{ 'id': 'uninitvar' }]
9+
project_path = tmp_path / 'project.cppcheck'
10+
create_gui_project_file(project_path, root_path=str(tmp_path), suppressions=suppressions)
11+
12+
# Uninitvar suppressed on command line before import
13+
args = ['--suppress=uninitvar', f'--project={project_path}']
14+
out_exp = [
15+
"cppcheck: error: suppression 'uninitvar' already exists",
16+
f"cppcheck: error: failed to load project '{project_path}'. An error occurred."
17+
]
18+
assert_cppcheck(args, ec_exp=1, out_exp=out_exp)
19+
20+
def test_multiple_cli_and_project_suppressions(tmp_path):
21+
# Uninitvar and unreadVariable suppressed in project file
22+
suppressions = [{ 'id': 'uninitvar' }, { 'id': 'unreadVariable' },]
23+
project_path = tmp_path / 'project.cppcheck'
24+
create_gui_project_file(project_path, root_path=str(tmp_path), suppressions=suppressions)
25+
26+
# Uninitvar and unreadVariable suppressed on command line before import
27+
args = ['--suppress=uninitvar', '--suppress=unreadVariable', f'--project={project_path}']
28+
out_exp = [
29+
"cppcheck: error: suppression 'uninitvar' already exists",
30+
"cppcheck: error: suppression 'unreadVariable' already exists",
31+
f"cppcheck: error: failed to load project '{project_path}'. An error occurred."
32+
]
33+
assert_cppcheck(args, ec_exp=1, out_exp=out_exp)

test/testimportproject.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class TestImportProject : public TestFixture {
7979
TEST_CASE(importCompileCommandsDirectoryMissing); // 'directory' field missing
8080
TEST_CASE(importCompileCommandsDirectoryInvalid); // 'directory' field not a string
8181
TEST_CASE(importCppcheckGuiProject);
82+
TEST_CASE(importCppcheckGuiProjectDuplicateSuppressions);
8283
TEST_CASE(importCppcheckGuiProjectPremiumMisra);
8384
TEST_CASE(ignorePaths);
8485
TEST_CASE(testVcxprojUnicode);
@@ -536,6 +537,26 @@ class TestImportProject : public TestFixture {
536537
ASSERT_EQUALS(true, s.inlineSuppressions);
537538
}
538539

540+
void importCppcheckGuiProjectDuplicateSuppressions() const {
541+
REDIRECT;
542+
constexpr char xml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
543+
"<project version=\"1\">\n"
544+
" <root name=\".\"/>\n"
545+
" <project-name>test test</project-name>\n"
546+
" <suppressions>\n"
547+
" <suppression>uninitvar</suppression>\n"
548+
" <suppression>uninitvar</suppression>\n"
549+
" </suppressions>\n"
550+
"</project>\n";
551+
std::istringstream istr(xml);
552+
Settings s;
553+
Suppressions supprs;
554+
TestImporter project;
555+
ASSERT_EQUALS(false, project.importCppcheckGuiProject(istr, s, supprs));
556+
ASSERT_EQUALS(1, project.errors.size());
557+
ASSERT_EQUALS("suppression 'uninitvar' already exists", project.errors[0]);
558+
}
559+
539560
void importCppcheckGuiProjectPremiumMisra() const {
540561
REDIRECT;
541562
constexpr char xml[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

0 commit comments

Comments
 (0)