From 0533d7bf9c664441fdb557f9ba52ea769c9a4851 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Sat, 27 Feb 2016 16:03:50 +0100 Subject: [PATCH] Run astyle + minor refactoring --- Makefile | 2 +- lib/check.h | 5 ++--- lib/checkclass.cpp | 4 ++-- lib/checkcondition.cpp | 2 +- lib/checkmemoryleak.cpp | 22 ++++++++++------------ lib/checkmemoryleak.h | 4 ++-- lib/errorlogger.cpp | 15 ++++++++------- lib/errorlogger.h | 10 +++++++--- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 828455a47..7ac076572 100644 --- a/Makefile +++ b/Makefile @@ -383,7 +383,7 @@ $(SRCDIR)/checkvaarg.o: lib/checkvaarg.cpp lib/cxx11emu.h lib/checkvaarg.h lib/c $(SRCDIR)/cppcheck.o: lib/cppcheck.cpp lib/cxx11emu.h lib/cppcheck.h lib/config.h lib/settings.h lib/library.h lib/mathlib.h lib/standards.h lib/errorlogger.h lib/suppressions.h lib/timer.h lib/check.h lib/token.h lib/valueflow.h lib/tokenize.h lib/tokenlist.h lib/preprocessor.h lib/path.h lib/version.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/cppcheck.o $(SRCDIR)/cppcheck.cpp -$(SRCDIR)/errorlogger.o: lib/errorlogger.cpp lib/cxx11emu.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/path.h lib/cppcheck.h lib/settings.h lib/library.h lib/mathlib.h lib/standards.h lib/timer.h lib/check.h lib/token.h lib/valueflow.h lib/tokenize.h lib/tokenlist.h +$(SRCDIR)/errorlogger.o: lib/errorlogger.cpp lib/cxx11emu.h lib/errorlogger.h lib/config.h lib/suppressions.h lib/path.h lib/cppcheck.h lib/settings.h lib/library.h lib/mathlib.h lib/standards.h lib/timer.h lib/check.h lib/token.h lib/valueflow.h lib/tokenize.h lib/tokenlist.h lib/utils.h $(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CFG) $(CXXFLAGS) $(UNDEF_STRICT_ANSI) -c -o $(SRCDIR)/errorlogger.o $(SRCDIR)/errorlogger.cpp $(SRCDIR)/library.o: lib/library.cpp lib/cxx11emu.h lib/library.h lib/config.h lib/mathlib.h lib/standards.h lib/errorlogger.h lib/suppressions.h lib/path.h lib/tokenlist.h lib/token.h lib/valueflow.h lib/symboldatabase.h lib/astutils.h diff --git a/lib/check.h b/lib/check.h index 8c7287987..637616efc 100644 --- a/lib/check.h +++ b/lib/check.h @@ -118,7 +118,7 @@ protected: /** report an error */ template void reportError(const Token *tok, const Severity::SeverityType severity, const T id, const U msg, const CWE &cwe, bool inconclusive) { - std::list callstack(1, tok); + const std::list callstack(1, tok); reportError(callstack, severity, id, msg, cwe, inconclusive); } @@ -131,8 +131,7 @@ protected: /** report an error */ template void reportError(const std::list &callstack, Severity::SeverityType severity, const T id, const U msg, const CWE &cwe, bool inconclusive) { - ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, cwe, inconclusive); - errmsg._cwe = cwe.id; + const ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, cwe, inconclusive); if (_errorLogger) _errorLogger->reportErr(errmsg); else diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index e0a7b6c6b..b0e0931e6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -364,7 +364,7 @@ void CheckClass::copyConstructorMallocError(const Token *cctor, const Token *all void CheckClass::copyConstructorShallowCopyError(const Token *tok, const std::string& varname) { - reportError(tok, Severity::style, "copyCtorPointerCopying", + reportError(tok, Severity::style, "copyCtorPointerCopying", "Value of pointer '" + varname + "', which points to allocated memory, is copied in copy constructor instead of allocating new memory.", CWE398, false); } @@ -1228,7 +1228,7 @@ void CheckClass::operatorEqReturnError(const Token *tok, const std::string &clas { reportError(tok, Severity::style, "operatorEq", "'" + className + "::operator=' should return '" + className + " &'.\n" "The "+className+"::operator= does not conform to standard C/C++ behaviour. To conform to standard C/C++ behaviour, return a reference to self (such as: '"+className+" &"+className+"::operator=(..) { .. return *this; }'. For safety reasons it might be better to not fix this message. If you think that safety is always more important than conformance then please ignore/suppress this message. For more details about this topic, see the book \"Effective C++\" by Scott Meyers." - , CWE398, false); + , CWE398, false); } //--------------------------------------------------------------------------- diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 4443d2723..f2d6b1b99 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -28,7 +28,7 @@ #include #include -// CWE ids used +// CWE ids used static const struct CWE CWE398(398U); static const struct CWE CWE570(570U); static const struct CWE CWE571(571U); diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 3db1318ed..9cc5c6239 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -272,7 +272,7 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A } //--------------------------------------------------------------------------- -void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, unsigned int cwe) const +void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const { std::list callstack; @@ -282,11 +282,9 @@ void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severit reportErr(callstack, severity, id, msg, cwe); } -void CheckMemoryLeak::reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, unsigned int cwe) const +void CheckMemoryLeak::reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const { - ErrorLogger::ErrorMessage errmsg(callstack, tokenizer?&tokenizer->list:0, severity, id, msg, false); - errmsg._cwe = cwe; - + const ErrorLogger::ErrorMessage errmsg(callstack, tokenizer?&tokenizer->list:0, severity, id, msg, cwe, false); if (errorLogger) errorLogger->reportErr(errmsg); else @@ -295,12 +293,12 @@ void CheckMemoryLeak::reportErr(const std::list &callstack, Sever void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname) const { - reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname, 401U); + reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname, CWE(401U)); } void CheckMemoryLeak::memleakUponReallocFailureError(const Token *tok, const std::string &varname) const { - reportErr(tok, Severity::error, "memleakOnRealloc", "Common realloc mistake: \'" + varname + "\' nulled but not freed upon failure", 401U); + reportErr(tok, Severity::error, "memleakOnRealloc", "Common realloc mistake: \'" + varname + "\' nulled but not freed upon failure", CWE(401U)); } void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname) const @@ -308,27 +306,27 @@ void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &var std::string errmsg("Resource leak"); if (!varname.empty()) errmsg += ": " + varname; - reportErr(tok, Severity::error, "resourceLeak", errmsg, 775U); + reportErr(tok, Severity::error, "resourceLeak", errmsg, CWE(775U)); } void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname) const { - reportErr(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname, 415U); + reportErr(tok, Severity::error, "deallocDealloc", "Deallocating a deallocated pointer: " + varname, CWE(415U)); } void CheckMemoryLeak::deallocuseError(const Token *tok, const std::string &varname) const { - reportErr(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released", 416U); + reportErr(tok, Severity::error, "deallocuse", "Dereferencing '" + varname + "' after it is deallocated / released", CWE(416U)); } void CheckMemoryLeak::mismatchSizeError(const Token *tok, const std::string &sz) const { - reportErr(tok, Severity::error, "mismatchSize", "The allocated size " + sz + " is not a multiple of the underlying type's size.", 131U); + reportErr(tok, Severity::error, "mismatchSize", "The allocated size " + sz + " is not a multiple of the underlying type's size.", CWE(131U)); } void CheckMemoryLeak::mismatchAllocDealloc(const std::list &callstack, const std::string &varname) const { - reportErr(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname, 762U); + reportErr(callstack, Severity::error, "mismatchAllocDealloc", "Mismatching allocation and deallocation: " + varname, CWE(762U)); } CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Function* func, std::list *callstack) const diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index 1d4e9eaaa..73504062c 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -75,7 +75,7 @@ private: * @param msg text * @param cwe cwe number */ - void reportErr(const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg, unsigned int cwe) const; + void reportErr(const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const; /** * Report error. Similar with the function Check::reportError @@ -85,7 +85,7 @@ private: * @param msg text * @param cwe cwe number */ - void reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, unsigned int cwe) const; + void reportErr(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe) const; public: CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e, const Settings *s) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 876e9097e..ef1bfd87f 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -21,6 +21,7 @@ #include "cppcheck.h" #include "tokenlist.h" #include "token.h" +#include "utils.h" #include @@ -136,7 +137,7 @@ std::string ErrorLogger::ErrorMessage::serialize() const std::ostringstream oss; oss << _id.length() << " " << _id; oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity); - oss << MathLib::toString(_cwe).length() << " " << MathLib::toString(_cwe); + oss << MathLib::toString(_cwe.id).length() << " " << MathLib::toString(_cwe.id); if (_inconclusive) { const std::string inconclusive("inconclusive"); oss << inconclusive.length() << " " << inconclusive; @@ -193,7 +194,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data) _id = results[0]; _severity = Severity::fromString(results[1]); std::istringstream scwe(results[2]); - scwe >> _cwe; + scwe >> _cwe.id; _shortMessage = results[3]; _verboseMessage = results[4]; @@ -209,7 +210,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data) iss.get(); std::string temp; for (unsigned int i = 0; i < len && iss.good(); ++i) { - char c = static_cast(iss.get()); + const char c = static_cast(iss.get()); temp.append(1, c); } @@ -311,8 +312,8 @@ std::string ErrorLogger::ErrorMessage::toXML(bool verbose, int version) const printer.PushAttribute("severity", Severity::toString(_severity).c_str()); printer.PushAttribute("msg", fixInvalidChars(_shortMessage).c_str()); printer.PushAttribute("verbose", fixInvalidChars(_verboseMessage).c_str()); - if (_cwe) - printer.PushAttribute("cwe", _cwe); + if (_cwe.id) + printer.PushAttribute("cwe", _cwe.id); if (_inconclusive) printer.PushAttribute("inconclusive", "true"); @@ -410,8 +411,8 @@ void ErrorLogger::reportUnmatchedSuppressions(const std::list callStack; - callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(i->file, i->line)); + const std::list callStack = make_container< std::list > () + << ErrorLogger::ErrorMessage::FileLocation(i->file, i->line); reportErr(ErrorLogger::ErrorMessage(callStack, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false)); } } diff --git a/lib/errorlogger.h b/lib/errorlogger.h index bbb956d8d..29aa61253 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -27,9 +27,13 @@ #include #include +/** + * CWE id (Common Weakness Enumeration) + * See https://cwe.mitre.org/ for further reference. + * */ struct CWE { - explicit CWE(unsigned short ID) : id(ID) {} - unsigned short id; + explicit CWE(unsigned short ID) : id(ID) {} + unsigned short id; }; @@ -239,7 +243,7 @@ public: std::string file0; Severity::SeverityType _severity; - unsigned int _cwe; + CWE _cwe; bool _inconclusive; /** set short and verbose messages */