Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11450 cppcheck: error: Failed to add suppression. No id. #6722

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
10 changes: 8 additions & 2 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ std::string SuppressionList::parseFile(std::istream &istr)
if (line.empty())
continue;

std::string::size_type pos = 0;
while (pos < line.size() && std::isspace(line[pos]))
++pos;
if (pos == line.size())
continue;

// Skip comments
if (line.length() > 1 && line[0] == '#')
if (line[pos] == '#')
continue;
if (line.length() >= 2 && line[0] == '/' && line[1] == '/')
if (pos < line.size() - 1 && line[pos] == '/' && line[pos + 1] == '/')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general I prefer pos + 1 < line.size() instead of pos < line.size() - 1 it's more robust in case the string is empty. but well in this particular case we do know that it's not empty.

continue;

const std::string errmsg(addSuppressionLine(line));
Expand Down
6 changes: 6 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,12 @@ class TestSuppressions : public TestFixture {
SuppressionList suppressions6;
ASSERT_EQUALS("", suppressions6.parseFile(file6));
ASSERT_EQUALS(true, suppressions6.isSuppressed(errorMessage("abc", "test.cpp", 123)));

std::istringstream file7(" // comment\n" // #11450
"abc");
SuppressionList suppressions7;
ASSERT_EQUALS("", suppressions7.parseFile(file7));
ASSERT_EQUALS(true, suppressions7.isSuppressed(errorMessage("abc", "test.cpp", 123)));
}

void inlinesuppress() const {
Expand Down
Loading