Skip to content

Commit 7e34983

Browse files
danmarDaniel Marjamäki
authored andcommitted
fix
1 parent 1937ce7 commit 7e34983

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ $(libcppdir)/cppcheck.o: lib/cppcheck.cpp externals/picojson/picojson.h external
600600
$(libcppdir)/ctu.o: lib/ctu.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/check.h lib/config.h lib/ctu.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
601601
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/ctu.cpp
602602

603-
$(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
603+
$(libcppdir)/errorlogger.o: lib/errorlogger.cpp externals/tinyxml2/tinyxml2.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h lib/xml.h
604604
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errorlogger.cpp
605605

606606
$(libcppdir)/errortypes.o: lib/errortypes.cpp lib/config.h lib/errortypes.h lib/utils.h

lib/errorlogger.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "path.h"
2424
#include "settings.h"
2525
#include "suppressions.h"
26+
#include "symboldatabase.h"
2627
#include "token.h"
2728
#include "tokenlist.h"
2829
#include "utils.h"
@@ -35,6 +36,7 @@
3536
#include <cstring>
3637
#include <fstream>
3738
#include <iomanip>
39+
#include <numeric>
3840
#include <sstream>
3941
#include <string>
4042
#include <unordered_map>
@@ -161,11 +163,12 @@ ErrorMessage::ErrorMessage(ErrorPath errorPath, const TokenList *tokenList, Seve
161163

162164
setmsg(msg);
163165

164-
std::list<const Token*> callstack;
165-
for (const ErrorPathItem& e: errorPath) {
166-
callstack.push_back(e.first);
167-
}
168-
calculateWarningHash(callstack);
166+
std::list<const Token*> tokens;
167+
std::transform(errorPath.cbegin(), errorPath.cend(), std::back_inserter(tokens),
168+
[](const ErrorPathItem& e) {
169+
return e.first;
170+
});
171+
calculateWarningHash(tokens);
169172
}
170173

171174
// TODO: improve errorhandling?
@@ -259,6 +262,8 @@ void ErrorMessage::calculateWarningHash(const std::list<const Token*>& callstack
259262
for (const Token* tok: callstack) {
260263
if (!tok)
261264
continue;
265+
if (!tok->scope())
266+
return; // might be a syntax error before scope info has been set
262267
if (tok->scope()->isExecutable()) {
263268
// Executable scope => include all tokens in the function => if the
264269
// function is changed the hash is changed
@@ -295,10 +300,9 @@ void ErrorMessage::calculateWarningHash(const std::list<const Token*>& callstack
295300

296301
// hash algorithm: sdbm
297302
// any hash algorithm can be used but it has to be the same hash on different platforms and compilers
298-
hash = 0;
299-
for (auto c: hashString) {
300-
hash = c + (hash << 6) + (hash << 16) - hash;
301-
}
303+
hash = std::accumulate(hashString.cbegin(), hashString.cend(), 0, [](std::size_t hash, char c) {
304+
return static_cast<unsigned char>(c) + (hash << 6) + (hash << 16) - hash;
305+
});
302306
}
303307

304308
static void serializeString(std::string &oss, const std::string & str)

oss-fuzz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ $(libcppdir)/cppcheck.o: ../lib/cppcheck.cpp ../externals/picojson/picojson.h ..
270270
$(libcppdir)/ctu.o: ../lib/ctu.cpp ../externals/tinyxml2/tinyxml2.h ../lib/astutils.h ../lib/check.h ../lib/config.h ../lib/ctu.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenize.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
271271
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/ctu.cpp
272272

273-
$(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml2.h ../lib/check.h ../lib/checkers.h ../lib/color.h ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/standards.h ../lib/suppressions.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
273+
$(libcppdir)/errorlogger.o: ../lib/errorlogger.cpp ../externals/tinyxml2/tinyxml2.h ../lib/check.h ../lib/checkers.h ../lib/color.h ../lib/config.h ../lib/cppcheck.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/suppressions.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h ../lib/xml.h
274274
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/errorlogger.cpp
275275

276276
$(libcppdir)/errortypes.o: ../lib/errortypes.cpp ../lib/config.h ../lib/errortypes.h ../lib/utils.h

test/cli/other_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2666,7 +2666,7 @@ def test_xml_output(tmp_path): # #13391 / #13485
26662666
<results version="2">
26672667
<cppcheck version="{}"/>
26682668
<errors>
2669-
<error id="nullPointerRedundantCheck" severity="warning" msg="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." verbose="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." cwe="476" file0="{}" remark="boom">
2669+
<error id="nullPointerRedundantCheck" severity="warning" msg="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." verbose="Either the condition &apos;p&apos; is redundant or there is possible null pointer dereference: p." cwe="476" hash="1842500187" file0="{}" remark="boom">
26702670
<location file="{}" line="5" column="12" info="Null pointer dereference"/>
26712671
<location file="{}" line="4" column="8" info="Assuming that condition &apos;p&apos; is not redundant"/>
26722672
<symbol>p</symbol>

0 commit comments

Comments
 (0)