Skip to content

Commit 460fde7

Browse files
authored
Add files via upload
1 parent 87ee784 commit 460fde7

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
...
2+
throw ("my exception!",546); // BBAD
3+
...
4+
throw errorFunc("my exception!",546); // GOOD
5+
...
6+
std::runtime_error("msg error"); // BAD
7+
...
8+
throw std::runtime_error("msg error"); // GOOD
9+
...
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Finding places for the dangerous use of exceptions.</p>
7+
8+
</overview>
9+
10+
<example>
11+
<p>The following example demonstrates erroneous and fixed methods for using exceptions.</p>
12+
<sample src="FindIncorrectlyUsedExceptions.cpp" />
13+
14+
</example>
15+
<references>
16+
17+
<li>
18+
CERT CPP Coding Standard:
19+
<a href="https://wiki.sei.cmu.edu/confluence/display/cplusplus/DCL57-CPP.+Do+not+let+exceptions+escape+from+destructors+or+deallocation+functions">DCL57-CPP. Do not let exceptions escape from destructors or deallocation functions</a>.
20+
</li>
21+
22+
</references>
23+
</qhelp>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name Operator Find Incorrectly Used Exceptions
3+
* @description --Finding places for the dangerous use of exceptions.
4+
* @kind problem
5+
* @id cpp/operator-find-incorrectly-used-exceptions
6+
* @problem.severity warning
7+
* @precision medium
8+
* @tags correctness
9+
* security
10+
* external/cwe/cwe-703
11+
* external/cwe/cwe-248
12+
* external/cwe/cwe-390
13+
*/
14+
15+
import cpp
16+
17+
from FunctionCall fc, string msg
18+
where
19+
exists(ThrowExpr texp |
20+
texp.getEnclosingFunction() = fc.getTarget() and
21+
(
22+
fc.getTarget().hasGlobalOrStdName("DllMain") and
23+
not exists(TryStmt ts |
24+
texp.getEnclosingStmt().getParentStmt*() = ts.getStmt() and
25+
not ts.getACatchClause().isEmpty()
26+
) and
27+
msg = "DllMain contains exeption no wrapped to try..catch blocks."
28+
or
29+
texp.getExpr().isParenthesised() and
30+
texp.getExpr().(CommaExpr).getLeftOperand().isConstant() and
31+
texp.getExpr().(CommaExpr).getRightOperand().isConstant() and
32+
msg = "There is an exception in the function that requires your attention."
33+
)
34+
)
35+
or
36+
fc.getTarget() instanceof Constructor and
37+
fc.getTargetType().(Class).getABaseClass+().hasGlobalOrStdName("exception") and
38+
not fc.isInMacroExpansion() and
39+
not exists(ThrowExpr texp | fc.getEnclosingStmt() = texp.getEnclosingStmt()) and
40+
not exists(FunctionCall fctmp | fctmp.getAnArgument() = fc) and
41+
not fc instanceof ConstructorDirectInit and
42+
not fc.getEnclosingStmt() instanceof DeclStmt and
43+
not fc instanceof ConstructorDelegationInit and
44+
msg = "This object does not generate an exception."
45+
select fc, msg

0 commit comments

Comments
 (0)