Skip to content

Commit aa6fa0e

Browse files
committed
Fix #14225 (addons; add optional "hash" attribute for warning messages)
1 parent 74b2c43 commit aa6fa0e

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

lib/cppcheck.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,9 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
16241624
}
16251625
errmsg.file0 = file0;
16261626

1627+
if (obj.count("hash")>0)
1628+
errmsg.hash = obj["hash"].get<std::int64_t>();
1629+
16271630
mErrorLogger.reportErr(errmsg);
16281631
}
16291632
}

lib/errorlogger.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const std::set<std::string> ErrorLogger::mCriticalErrorIds{
5757
};
5858

5959
ErrorMessage::ErrorMessage()
60-
: severity(Severity::none), cwe(0U), certainty(Certainty::normal), hash(0)
60+
: severity(Severity::none), cwe(0U), certainty(Certainty::normal)
6161
{}
6262

6363
// TODO: id and msg are swapped compared to other calls
@@ -67,8 +67,7 @@ ErrorMessage::ErrorMessage(std::list<FileLocation> callStack, std::string file1,
6767
file0(std::move(file1)),
6868
severity(severity), // severity for this error message
6969
cwe(0U),
70-
certainty(certainty),
71-
hash(0)
70+
certainty(certainty)
7271
{
7372
// set the summary and verbose messages
7473
setmsg(msg);
@@ -82,15 +81,14 @@ ErrorMessage::ErrorMessage(std::list<FileLocation> callStack, std::string file1,
8281
file0(std::move(file1)),
8382
severity(severity), // severity for this error message
8483
cwe(cwe.id),
85-
certainty(certainty),
86-
hash(0)
84+
certainty(certainty)
8785
{
8886
// set the summary and verbose messages
8987
setmsg(msg);
9088
}
9189

9290
ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity severity, std::string id, const std::string& msg, Certainty certainty)
93-
: id(std::move(id)), severity(severity), cwe(0U), certainty(certainty), hash(0)
91+
: id(std::move(id)), severity(severity), cwe(0U), certainty(certainty)
9492
{
9593
// Format callstack
9694
for (auto it = callstack.cbegin(); it != callstack.cend(); ++it) {
@@ -125,7 +123,7 @@ ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const Token
125123

126124
setmsg(msg);
127125

128-
hash = 0; // calculateWarningHash(list, hashWarning.str());
126+
// hash = calculateWarningHash(list, hashWarning.str());
129127
}
130128

131129
ErrorMessage::ErrorMessage(ErrorPath errorPath, const TokenList *tokenList, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
@@ -158,7 +156,7 @@ ErrorMessage::ErrorMessage(ErrorPath errorPath, const TokenList *tokenList, Seve
158156

159157
setmsg(msg);
160158

161-
hash = 0; // calculateWarningHash(tokenList, hashWarning.str());
159+
// hash = calculateWarningHash(tokenList, hashWarning.str());
162160
}
163161

164162
ErrorMessage::ErrorMessage(const tinyxml2::XMLElement * const errmsg)

lib/errorlogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class CPPCHECKLIB ErrorMessage {
179179
std::string guideline;
180180

181181
/** Warning hash */
182-
std::size_t hash;
182+
std::size_t hash{};
183183

184184
/** set short and verbose messages */
185185
void setmsg(const std::string &msg);

lib/sarifreport.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ picojson::array SarifReport::serializeResults() const
145145
message["text"] = picojson::value(finding.shortMessage());
146146
res["message"] = picojson::value(message);
147147
res["ruleId"] = picojson::value(finding.id);
148+
if (finding.hash != 0) {
149+
picojson::object partialFingerprints;
150+
partialFingerprints["hash"] = picojson::value(std::to_string(finding.hash));
151+
partialFingerprints["id"] = picojson::value(finding.id);
152+
res["partialFingerprints"] = picojson::value(partialFingerprints);
153+
}
148154
results.emplace_back(res);
149155
}
150156
return results;

test/cli/premium_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,21 @@ def test_help(tmpdir):
163163
assert exitcode == 0
164164
assert stdout.startswith('Cppcheck ') # check for product name - TODO: should be "Cppcheck Premium"
165165
assert '--premium=' in stdout, stdout # check for premium option
166-
assert 'cppchecksolutions.com' in stdout, stdout # check for premium help link
166+
assert 'cppchecksolutions.com' in stdout, stdout # check for premium help link
167+
168+
169+
def test_hash(tmpdir):
170+
# Trac 14225 - warnings with hash
171+
test_file = os.path.join(tmpdir, 'test.c')
172+
addon_file = os.path.join(tmpdir, 'premiumaddon.py')
173+
174+
with open(test_file, 'wt') as f:
175+
f.write('void foo();\n')
176+
177+
args = [f"--addon={addon_file}", '--xml', test_file]
178+
179+
with open(addon_file, 'wt') as f:
180+
f.write('print(\'{"addon":"a","column":1,"errorId":"id","extra":"","file":"test.c","hash":123,"linenr":1,"message":"bug","severity":"error"}\')')
181+
182+
_, _, stderr = cppcheck(args)
183+
assert '<error id="a-id" severity="error" msg="bug" verbose="bug" hash="123" ' in stderr

0 commit comments

Comments
 (0)