Skip to content

Commit c33a2f9

Browse files
committed
Fix #14225 (addons; add optional "hash" attribute for warning messages)
1 parent 3e8c9d4 commit c33a2f9

5 files changed

Lines changed: 33 additions & 9 deletions

File tree

lib/cppcheck.cpp

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

1621+
if (obj.count("hash")>0)
1622+
errmsg.hash = obj["hash"].get<std::int64_t>();
1623+
16211624
mErrorLogger.reportErr(errmsg);
16221625
}
16231626
}

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,20 @@ def test_invalid_license_retry(tmpdir):
156156

157157
_, _, stderr = cppcheck(args)
158158
assert 'Invalid license' not in stderr
159+
160+
161+
def test_hash(tmpdir):
162+
# Trac 14225 - warnings with hash
163+
test_file = os.path.join(tmpdir, 'test.c')
164+
addon_file = os.path.join(tmpdir, 'premiumaddon.py')
165+
166+
with open(test_file, 'wt') as f:
167+
f.write('void foo();\n')
168+
169+
args = [f"--addon={addon_file}", '--xml', test_file]
170+
171+
with open(addon_file, 'wt') as f:
172+
f.write('print(\'{"addon":"a","column":1,"errorId":"id","extra":"","file":"test.c","hash":123,"linenr":1,"message":"bug","severity":"error"}\')')
173+
174+
_, _, stderr = cppcheck(args)
175+
assert '<error id="a-id" severity="error" msg="bug" verbose="bug" hash="123" ' in stderr

0 commit comments

Comments
 (0)