diff --git a/lib/suppressions.cpp b/lib/suppressions.cpp index d395a4c1e..af5470423 100644 --- a/lib/suppressions.cpp +++ b/lib/suppressions.cpp @@ -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); } } @@ -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 + "\""; } } diff --git a/man/manual.md b/man/manual.md index 332bc9a2a..b0a9a6088 100644 --- a/man/manual.md +++ b/man/manual.md @@ -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 diff --git a/test/testsuppressions.cpp b/test/testsuppressions.cpp index 87aee88a0..2c8eccf06 100644 --- a/test/testsuppressions.cpp +++ b/test/testsuppressions.cpp @@ -51,6 +51,7 @@ private: 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); @@ -171,6 +172,14 @@ private: } } + 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;