Skip to content

Commit

Permalink
Fix #12181 (Suppressions: allow that id with * is added) (#5681)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar authored Nov 20, 2023
1 parent 3bafe16 commit 036df0a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
17 changes: 8 additions & 9 deletions lib/suppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ static bool isAcceptedErrorIdChar(char c)
case '_':
case '-':
case '.':
case '*':
return true;
default:
return std::isalnum(c);
return c > 0 && std::isalnum(c);
}
}

Expand Down Expand Up @@ -255,14 +256,12 @@ std::string Suppressions::addSuppression(Suppressions::Suppression suppression)
if (suppression.errorId.empty() && suppression.hash == 0)
return "Failed to add suppression. No id.";

if (suppression.errorId != "*") {
for (std::string::size_type pos = 0; pos < suppression.errorId.length(); ++pos) {
if (suppression.errorId[pos] < 0 || !isAcceptedErrorIdChar(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
if (pos == 0 && std::isdigit(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
for (std::string::size_type pos = 0; pos < suppression.errorId.length(); ++pos) {
if (!isAcceptedErrorIdChar(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
if (pos == 0 && std::isdigit(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
}

Expand Down
6 changes: 3 additions & 3 deletions man/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ The format for an error suppression is one of:
[error id]:[filename2]
[error id]

The `error id` is the id that you want to suppress. The easiest way to get it is to use the --template=gcc command line flag. The id is shown in brackets.
The `error id` is the id that you want to suppress. The id of a warning is shown in brackets in the normal cppcheck text output. The suppression `error id` may contain \* to match any sequence of tokens.

The filename may include the wildcard characters \* or ?, which matches any sequence of characters or any single character respectively.
It is recommended to use "/" as path separator on all operating systems. The filename must match the filename in the reported warning exactly.
The filename may include the wildcard characters \* or ?, which matches any sequence of characters or any single character respectively.
It is recommended to use forward-slash `/` as path separator on all operating systems. The filename must match the filename in the reported warning exactly.
For instance, if the warning contains a relative path, then the suppression must match that relative path.

## Command line suppression
Expand Down
9 changes: 9 additions & 0 deletions test/testsuppressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class TestSuppressions : public TestFixture {
TEST_CASE(suppressionsDosFormat); // Ticket #1836
TEST_CASE(suppressionsFileNameWithColon); // Ticket #1919 - filename includes colon
TEST_CASE(suppressionsGlob);
TEST_CASE(suppressionsGlobId);
TEST_CASE(suppressionsFileNameWithExtraPath);
TEST_CASE(suppressionsSettings);
TEST_CASE(suppressionsSettingsThreads);
Expand Down Expand Up @@ -171,6 +172,14 @@ class TestSuppressions : public TestFixture {
}
}

void suppressionsGlobId() const {
Suppressions suppressions;
std::istringstream s("a*\n");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed(errorMessage("abc", "xyz.cpp", 1)));
ASSERT_EQUALS(false, suppressions.isSuppressed(errorMessage("def", "xyz.cpp", 1)));
}

void suppressionsFileNameWithExtraPath() const {
// Ticket #2797
Suppressions suppressions;
Expand Down

0 comments on commit 036df0a

Please sign in to comment.