|
| 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