Moving error reporting functions from header to cpp (#3279)
This commit is contained in:
parent
06c4542ac2
commit
f64011b669
|
@ -72,7 +72,13 @@ void CheckExceptionSafety::destructors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::destructorsError(const Token * const tok, const std::string &className) {
|
||||||
|
reportError(tok, Severity::warning, "exceptThrowInDestructor",
|
||||||
|
"Class " + className + " is not safe, destructor throws exception\n"
|
||||||
|
"The class " + className + " is not safe because its destructor "
|
||||||
|
"throws an exception. If " + className + " is used and an exception "
|
||||||
|
"is thrown that is caught in an outer scope the program will terminate.", CWE398, Certainty::normal);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void CheckExceptionSafety::deallocThrow()
|
void CheckExceptionSafety::deallocThrow()
|
||||||
|
@ -136,6 +142,11 @@ void CheckExceptionSafety::deallocThrow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::deallocThrowError(const Token * const tok, const std::string &varname) {
|
||||||
|
reportError(tok, Severity::warning, "exceptDeallocThrow", "Exception thrown in invalid state, '" +
|
||||||
|
varname + "' points at deallocated memory.", CWE398, Certainty::normal);
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// catch(const exception & err)
|
// catch(const exception & err)
|
||||||
// {
|
// {
|
||||||
|
@ -173,6 +184,13 @@ void CheckExceptionSafety::checkRethrowCopy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::rethrowCopyError(const Token * const tok, const std::string &varname) {
|
||||||
|
reportError(tok, Severity::style, "exceptRethrowCopy",
|
||||||
|
"Throwing a copy of the caught exception instead of rethrowing the original exception.\n"
|
||||||
|
"Rethrowing an exception with 'throw " + varname + ";' creates an unnecessary copy of '" + varname + "'. "
|
||||||
|
"To rethrow the caught exception without unnecessary copying or slicing, use a bare 'throw;'.", CWE398, Certainty::normal);
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
// try {} catch (std::exception err) {} <- Should be "std::exception& err"
|
// try {} catch (std::exception err) {} <- Should be "std::exception& err"
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
@ -195,6 +213,13 @@ void CheckExceptionSafety::checkCatchExceptionByValue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::catchExceptionByValueError(const Token *tok) {
|
||||||
|
reportError(tok, Severity::style,
|
||||||
|
"catchExceptionByValue", "Exception should be caught by reference.\n"
|
||||||
|
"The exception is caught by value. It could be caught "
|
||||||
|
"as a (const) reference which is usually recommended in C++.", CWE398, Certainty::normal);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static const Token * functionThrowsRecursive(const Function * function, std::set<const Function *> & recursive)
|
static const Token * functionThrowsRecursive(const Function * function, std::set<const Function *> & recursive)
|
||||||
{
|
{
|
||||||
|
@ -273,6 +298,10 @@ void CheckExceptionSafety::nothrowThrows()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::noexceptThrowError(const Token * const tok) {
|
||||||
|
reportError(tok, Severity::error, "throwInNoexceptFunction", "Exception thrown in function declared not to throw exceptions.", CWE398, Certainty::normal);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
// void func() { functionWithExceptionSpecification(); }
|
// void func() { functionWithExceptionSpecification(); }
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
@ -305,6 +334,15 @@ void CheckExceptionSafety::unhandledExceptionSpecification()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckExceptionSafety::unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) {
|
||||||
|
const std::string str1(tok1 ? tok1->str() : "foo");
|
||||||
|
const std::list<const Token*> locationList = { tok1, tok2 };
|
||||||
|
reportError(locationList, Severity::style, "unhandledExceptionSpecification",
|
||||||
|
"Unhandled exception specification when calling function " + str1 + "().\n"
|
||||||
|
"Unhandled exception specification when calling function " + str1 + "(). "
|
||||||
|
"Either use a try/catch around the function call, or add a exception specification for " + funcname + "() also.", CWE703, Certainty::inconclusive);
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
// 7.6.18.4 If no exception is presently being handled, evaluating a throw-expression with no operand calls std::terminate().
|
// 7.6.18.4 If no exception is presently being handled, evaluating a throw-expression with no operand calls std::terminate().
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
|
|
@ -99,47 +99,13 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** Don't throw exceptions in destructors */
|
/** Don't throw exceptions in destructors */
|
||||||
void destructorsError(const Token * const tok, const std::string &className) {
|
void destructorsError(const Token * const tok, const std::string &className);
|
||||||
reportError(tok, Severity::warning, "exceptThrowInDestructor",
|
void deallocThrowError(const Token * const tok, const std::string &varname);
|
||||||
"Class " + className + " is not safe, destructor throws exception\n"
|
void rethrowCopyError(const Token * const tok, const std::string &varname);
|
||||||
"The class " + className + " is not safe because its destructor "
|
void catchExceptionByValueError(const Token *tok);
|
||||||
"throws an exception. If " + className + " is used and an exception "
|
void noexceptThrowError(const Token * const tok);
|
||||||
"is thrown that is caught in an outer scope the program will terminate.", CWE398, Certainty::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deallocThrowError(const Token * const tok, const std::string &varname) {
|
|
||||||
reportError(tok, Severity::warning, "exceptDeallocThrow", "Exception thrown in invalid state, '" +
|
|
||||||
varname + "' points at deallocated memory.", CWE398, Certainty::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rethrowCopyError(const Token * const tok, const std::string &varname) {
|
|
||||||
reportError(tok, Severity::style, "exceptRethrowCopy",
|
|
||||||
"Throwing a copy of the caught exception instead of rethrowing the original exception.\n"
|
|
||||||
"Rethrowing an exception with 'throw " + varname + ";' creates an unnecessary copy of '" + varname + "'. "
|
|
||||||
"To rethrow the caught exception without unnecessary copying or slicing, use a bare 'throw;'.", CWE398, Certainty::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void catchExceptionByValueError(const Token *tok) {
|
|
||||||
reportError(tok, Severity::style,
|
|
||||||
"catchExceptionByValue", "Exception should be caught by reference.\n"
|
|
||||||
"The exception is caught by value. It could be caught "
|
|
||||||
"as a (const) reference which is usually recommended in C++.", CWE398, Certainty::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void noexceptThrowError(const Token * const tok) {
|
|
||||||
reportError(tok, Severity::error, "throwInNoexceptFunction", "Exception thrown in function declared not to throw exceptions.", CWE398, Certainty::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Missing exception specification */
|
/** Missing exception specification */
|
||||||
void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) {
|
void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname);
|
||||||
const std::string str1(tok1 ? tok1->str() : "foo");
|
|
||||||
const std::list<const Token*> locationList = { tok1, tok2 };
|
|
||||||
reportError(locationList, Severity::style, "unhandledExceptionSpecification",
|
|
||||||
"Unhandled exception specification when calling function " + str1 + "().\n"
|
|
||||||
"Unhandled exception specification when calling function " + str1 + "(). "
|
|
||||||
"Either use a try/catch around the function call, or add a exception specification for " + funcname + "() also.", CWE703, Certainty::inconclusive);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Rethrow without currently handled exception */
|
/** Rethrow without currently handled exception */
|
||||||
void rethrowNoCurrentExceptionError(const Token *tok);
|
void rethrowNoCurrentExceptionError(const Token *tok);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue