Skip to content

Commit 0ff4f9c

Browse files
committed
fixed #14309 - report inline suppressions without an error ID [skip ci]
1 parent 4a22256 commit 0ff4f9c

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

lib/preprocessor.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
8888
const std::string::size_type pos1 = comment.find_first_not_of("/* \t");
8989
if (pos1 == std::string::npos)
9090
return false;
91-
if (pos1 + cppchecksuppress.size() >= comment.size())
91+
if (pos1 + cppchecksuppress.size() >= comment.size()) {
92+
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
9293
return false;
94+
}
9395
if (comment.substr(pos1, cppchecksuppress.size()) != cppchecksuppress)
9496
return false;
9597

@@ -98,8 +100,10 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
98100

99101
// skip spaces after "cppcheck-suppress" and its possible prefix
100102
const std::string::size_type pos2 = comment.find_first_not_of(' ', posEndComment);
101-
if (pos2 == std::string::npos)
103+
if (pos2 == std::string::npos) {
104+
bad.emplace_back(tok->location.file(), tok->location.line, 0, "suppression without error ID");
102105
return false;
106+
}
103107

104108
SuppressionList::Type errorType = SuppressionList::Type::unique;
105109

@@ -142,9 +146,11 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
142146
s.lineNumber = tok->location.line;
143147
}
144148

149+
// TODO: unreachable?
145150
if (!errmsg.empty())
146151
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
147152

153+
// TODO: report ones without ID - unreachable?
148154
std::copy_if(suppressions.cbegin(), suppressions.cend(), std::back_inserter(inlineSuppressions), [](const SuppressionList::Suppression& s) {
149155
return !s.errorId.empty();
150156
});
@@ -159,9 +165,11 @@ static bool parseInlineSuppressionCommentToken(const simplecpp::Token *tok, std:
159165
s.type = errorType;
160166
s.lineNumber = tok->location.line;
161167

168+
// TODO: report when no ID - unreachable?
162169
if (!s.errorId.empty())
163170
inlineSuppressions.push_back(std::move(s));
164171

172+
// TODO: unreachable?
165173
if (!errmsg.empty())
166174
bad.emplace_back(tok->location.file(), tok->location.line, tok->location.col, std::move(errmsg));
167175
}

test/testsuppressions.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,37 @@ class TestSuppressions : public TestFixture {
716716
"[test.cpp:6:0]: (error) unknown suppression type 'cppcheck-suppress-end-unknown' [invalidSuppression]\n"
717717
"[test.cpp:4:0]: (error) Suppress Begin: No matching end [invalidSuppression]\n", errout_str());
718718

719+
ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress-file\n"
720+
"// cppcheck-suppress\n"
721+
"// cppcheck-suppress \n"
722+
"// cppcheck-suppress\t\n"
723+
"// cppcheck-suppress-begin\n"
724+
"// cppcheck-suppress-begin id0\n"
725+
"void f() {}\n"
726+
"// cppcheck-suppress-end\n",
727+
""));
728+
ASSERT_EQUALS("[test.cpp:1:0]: (error) suppression without error ID [invalidSuppression]\n"
729+
"[test.cpp:2:0]: (error) suppression without error ID [invalidSuppression]\n"
730+
"[test.cpp:3:0]: (error) suppression without error ID [invalidSuppression]\n"
731+
"[test.cpp:4:0]: (error) suppression without error ID [invalidSuppression]\n"
732+
"[test.cpp:5:0]: (error) suppression without error ID [invalidSuppression]\n"
733+
"[test.cpp:8:0]: (error) suppression without error ID [invalidSuppression]\n"
734+
"[test.cpp:6:0]: (error) Suppress Begin: No matching end [invalidSuppression]\n", errout_str());
735+
736+
ASSERT_EQUALS(1, (this->*check)("// cppcheck-suppress:\n"
737+
"// cppcheck-suppress-unknown\n"
738+
"// cppcheck-suppress-begin-unknown\n"
739+
"// cppcheck-suppress-begin\n"
740+
"void f() {}\n"
741+
"// cppcheck-suppress-end-unknown\n",
742+
""));
743+
// TODO: actually these are all invalid types
744+
ASSERT_EQUALS("[test.cpp:1:0]: (error) suppression without error ID [invalidSuppression]\n"
745+
"[test.cpp:2:0]: (error) suppression without error ID [invalidSuppression]\n"
746+
"[test.cpp:3:0]: (error) suppression without error ID [invalidSuppression]\n"
747+
"[test.cpp:4:0]: (error) suppression without error ID [invalidSuppression]\n"
748+
"[test.cpp:6:0]: (error) suppression without error ID [invalidSuppression]\n", errout_str());
749+
719750
ASSERT_EQUALS(1, (this->*check)("void f() {\n"
720751
" int a;\n"
721752
" // cppcheck-suppress-begin uninitvar\n"

0 commit comments

Comments
 (0)