Skip to content

Commit f64011b

Browse files
authored
Moving error reporting functions from header to cpp (danmar#3279)
1 parent 06c4542 commit f64011b

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

lib/checkexceptionsafety.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ void CheckExceptionSafety::destructors()
7272
}
7373
}
7474

75-
75+
void CheckExceptionSafety::destructorsError(const Token * const tok, const std::string &className) {
76+
reportError(tok, Severity::warning, "exceptThrowInDestructor",
77+
"Class " + className + " is not safe, destructor throws exception\n"
78+
"The class " + className + " is not safe because its destructor "
79+
"throws an exception. If " + className + " is used and an exception "
80+
"is thrown that is caught in an outer scope the program will terminate.", CWE398, Certainty::normal);
81+
}
7682

7783

7884
void CheckExceptionSafety::deallocThrow()
@@ -136,6 +142,11 @@ void CheckExceptionSafety::deallocThrow()
136142
}
137143
}
138144

145+
void CheckExceptionSafety::deallocThrowError(const Token * const tok, const std::string &varname) {
146+
reportError(tok, Severity::warning, "exceptDeallocThrow", "Exception thrown in invalid state, '" +
147+
varname + "' points at deallocated memory.", CWE398, Certainty::normal);
148+
}
149+
139150
//---------------------------------------------------------------------------
140151
// catch(const exception & err)
141152
// {
@@ -173,6 +184,13 @@ void CheckExceptionSafety::checkRethrowCopy()
173184
}
174185
}
175186

187+
void CheckExceptionSafety::rethrowCopyError(const Token * const tok, const std::string &varname) {
188+
reportError(tok, Severity::style, "exceptRethrowCopy",
189+
"Throwing a copy of the caught exception instead of rethrowing the original exception.\n"
190+
"Rethrowing an exception with 'throw " + varname + ";' creates an unnecessary copy of '" + varname + "'. "
191+
"To rethrow the caught exception without unnecessary copying or slicing, use a bare 'throw;'.", CWE398, Certainty::normal);
192+
}
193+
176194
//---------------------------------------------------------------------------
177195
// try {} catch (std::exception err) {} <- Should be "std::exception& err"
178196
//---------------------------------------------------------------------------
@@ -195,6 +213,13 @@ void CheckExceptionSafety::checkCatchExceptionByValue()
195213
}
196214
}
197215

216+
void CheckExceptionSafety::catchExceptionByValueError(const Token *tok) {
217+
reportError(tok, Severity::style,
218+
"catchExceptionByValue", "Exception should be caught by reference.\n"
219+
"The exception is caught by value. It could be caught "
220+
"as a (const) reference which is usually recommended in C++.", CWE398, Certainty::normal);
221+
}
222+
198223

199224
static const Token * functionThrowsRecursive(const Function * function, std::set<const Function *> & recursive)
200225
{
@@ -273,6 +298,10 @@ void CheckExceptionSafety::nothrowThrows()
273298
}
274299
}
275300

301+
void CheckExceptionSafety::noexceptThrowError(const Token * const tok) {
302+
reportError(tok, Severity::error, "throwInNoexceptFunction", "Exception thrown in function declared not to throw exceptions.", CWE398, Certainty::normal);
303+
}
304+
276305
//--------------------------------------------------------------------------
277306
// void func() { functionWithExceptionSpecification(); }
278307
//--------------------------------------------------------------------------
@@ -305,6 +334,15 @@ void CheckExceptionSafety::unhandledExceptionSpecification()
305334
}
306335
}
307336

337+
void CheckExceptionSafety::unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) {
338+
const std::string str1(tok1 ? tok1->str() : "foo");
339+
const std::list<const Token*> locationList = { tok1, tok2 };
340+
reportError(locationList, Severity::style, "unhandledExceptionSpecification",
341+
"Unhandled exception specification when calling function " + str1 + "().\n"
342+
"Unhandled exception specification when calling function " + str1 + "(). "
343+
"Either use a try/catch around the function call, or add a exception specification for " + funcname + "() also.", CWE703, Certainty::inconclusive);
344+
}
345+
308346
//--------------------------------------------------------------------------
309347
// 7.6.18.4 If no exception is presently being handled, evaluating a throw-expression with no operand calls std​::​​terminate().
310348
//--------------------------------------------------------------------------

lib/checkexceptionsafety.h

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,47 +99,13 @@ class CPPCHECKLIB CheckExceptionSafety : public Check {
9999

100100
private:
101101
/** Don't throw exceptions in destructors */
102-
void destructorsError(const Token * const tok, const std::string &className) {
103-
reportError(tok, Severity::warning, "exceptThrowInDestructor",
104-
"Class " + className + " is not safe, destructor throws exception\n"
105-
"The class " + className + " is not safe because its destructor "
106-
"throws an exception. If " + className + " is used and an exception "
107-
"is thrown that is caught in an outer scope the program will terminate.", CWE398, Certainty::normal);
108-
}
109-
110-
void deallocThrowError(const Token * const tok, const std::string &varname) {
111-
reportError(tok, Severity::warning, "exceptDeallocThrow", "Exception thrown in invalid state, '" +
112-
varname + "' points at deallocated memory.", CWE398, Certainty::normal);
113-
}
114-
115-
void rethrowCopyError(const Token * const tok, const std::string &varname) {
116-
reportError(tok, Severity::style, "exceptRethrowCopy",
117-
"Throwing a copy of the caught exception instead of rethrowing the original exception.\n"
118-
"Rethrowing an exception with 'throw " + varname + ";' creates an unnecessary copy of '" + varname + "'. "
119-
"To rethrow the caught exception without unnecessary copying or slicing, use a bare 'throw;'.", CWE398, Certainty::normal);
120-
}
121-
122-
void catchExceptionByValueError(const Token *tok) {
123-
reportError(tok, Severity::style,
124-
"catchExceptionByValue", "Exception should be caught by reference.\n"
125-
"The exception is caught by value. It could be caught "
126-
"as a (const) reference which is usually recommended in C++.", CWE398, Certainty::normal);
127-
}
128-
129-
void noexceptThrowError(const Token * const tok) {
130-
reportError(tok, Severity::error, "throwInNoexceptFunction", "Exception thrown in function declared not to throw exceptions.", CWE398, Certainty::normal);
131-
}
132-
102+
void destructorsError(const Token * const tok, const std::string &className);
103+
void deallocThrowError(const Token * const tok, const std::string &varname);
104+
void rethrowCopyError(const Token * const tok, const std::string &varname);
105+
void catchExceptionByValueError(const Token *tok);
106+
void noexceptThrowError(const Token * const tok);
133107
/** Missing exception specification */
134-
void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname) {
135-
const std::string str1(tok1 ? tok1->str() : "foo");
136-
const std::list<const Token*> locationList = { tok1, tok2 };
137-
reportError(locationList, Severity::style, "unhandledExceptionSpecification",
138-
"Unhandled exception specification when calling function " + str1 + "().\n"
139-
"Unhandled exception specification when calling function " + str1 + "(). "
140-
"Either use a try/catch around the function call, or add a exception specification for " + funcname + "() also.", CWE703, Certainty::inconclusive);
141-
}
142-
108+
void unhandledExceptionSpecificationError(const Token * const tok1, const Token * const tok2, const std::string & funcname);
143109
/** Rethrow without currently handled exception */
144110
void rethrowNoCurrentExceptionError(const Token *tok);
145111

0 commit comments

Comments
 (0)