|
| 1 | +/* |
| 2 | + * A library customize models that model the flow of exceptions through the program. |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +private import codingstandards.cpp.exceptions.ExceptionFlow |
| 7 | + |
| 8 | +/** A `ThrowingExpr` which is the origin of a exceptions in the program. */ |
| 9 | +abstract class OriginThrowingExpr extends ThrowingExpr { } |
| 10 | + |
| 11 | +/** |
| 12 | + * A `FunctionCall` to an external function without an exception specification that * |
| 13 | + * may throw an exception. |
| 14 | + */ |
| 15 | +abstract class ExternalUnderspecifiedFunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { } |
| 16 | + |
| 17 | +/** |
| 18 | + * An extensible predicate that describes functions that when called may throw an exception. |
| 19 | + */ |
| 20 | +extensible predicate throwingFunctionModel( |
| 21 | + string functionNamespaceQualifier, string functionTypeQualifier, string functionName, |
| 22 | + string exceptionNamespaceQualifier, string exceptionType |
| 23 | +); |
| 24 | + |
| 25 | +/** |
| 26 | + * A `FunctionCall` that may throw an exception of type `ExceptionType` as provded by |
| 27 | + * the extensible predicate `throwingFunctionModel`. |
| 28 | + */ |
| 29 | +private class ExternalFunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { |
| 30 | + ExceptionType exceptionType; |
| 31 | + |
| 32 | + ExternalFunctionCallThrowingExpr() { |
| 33 | + exists( |
| 34 | + string functionNamespaceQualifier, string functionTypeQualifier, string functionName, |
| 35 | + string exceptionNamespaceQualifier, string exceptionTypeSpec |
| 36 | + | |
| 37 | + throwingFunctionModel(functionNamespaceQualifier, functionTypeQualifier, functionName, |
| 38 | + exceptionNamespaceQualifier, exceptionTypeSpec) and |
| 39 | + this.getTarget() |
| 40 | + .hasQualifiedName(functionNamespaceQualifier, functionTypeQualifier, functionName) and |
| 41 | + exceptionType.(Class).hasQualifiedName(exceptionNamespaceQualifier, exceptionTypeSpec) |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + override ExceptionType getAnExceptionType() { result = exceptionType } |
| 46 | +} |
| 47 | + |
| 48 | +/** An expression which directly throws. */ |
| 49 | +class DirectThrowExprThrowingExpr extends DirectThrowExpr, OriginThrowingExpr { |
| 50 | + override ExceptionType getAnExceptionType() { result = getExceptionType() } |
| 51 | +} |
| 52 | + |
| 53 | +/** A `ReThrowExpr` which throws a previously caught exception. */ |
| 54 | +class ReThrowExprThrowingExpr extends ReThrowExpr, ThrowingExpr { |
| 55 | + predicate rethrows(CatchBlock cb, ExceptionType et, ThrowingExpr te) { |
| 56 | + // Find the nearest CatchBlock |
| 57 | + cb = getNearestCatch(this.getEnclosingStmt()) and |
| 58 | + // Find an `ExceptionType` which is caught by this catch block, and `ThrowingExpr` which throws that exception type |
| 59 | + catches(cb, te, et) |
| 60 | + } |
| 61 | + |
| 62 | + override ExceptionType getAnExceptionType() { rethrows(_, result, _) } |
| 63 | + |
| 64 | + CatchBlock getCatchBlock() { rethrows(result, _, _) } |
| 65 | +} |
| 66 | + |
| 67 | +/** An expression which calls a function which may throw an exception. */ |
| 68 | +class FunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { |
| 69 | + override ExceptionType getAnExceptionType() { |
| 70 | + exists(Function target | |
| 71 | + target = getTarget() and |
| 72 | + result = getAFunctionThrownType(target, _) and |
| 73 | + // [expect.spec] states that throwing an exception type that is prohibited |
| 74 | + // by the specification will result in the program terminating, unless |
| 75 | + // a custom `unexpected_handler` is registered that throws an exception type |
| 76 | + // which is compatible with the dynamic exception specification, or the |
| 77 | + // dynamic exception specification lists `std::bad_exception`, in which case |
| 78 | + // a `std::bad_exception` is thrown. |
| 79 | + // As dynamic exception specifications and the `unexpected_handler` are both |
| 80 | + // deprecated in C++14 and removed in C++17, we assume a default |
| 81 | + // `std::unexpected` handler that calls `std::terminate` and therefore |
| 82 | + // do not propagate such exceptions to the call sites for the function. |
| 83 | + not ( |
| 84 | + hasDynamicExceptionSpecification(target) and |
| 85 | + not result = getAHandledExceptionType(target.getAThrownType()) |
| 86 | + or |
| 87 | + isNoExceptTrue(target) |
| 88 | + ) |
| 89 | + ) |
| 90 | + or |
| 91 | + result = this.(ExternalUnderspecifiedFunctionCallThrowingExpr).getAnExceptionType() |
| 92 | + or |
| 93 | + result = this.(ExternalFunctionCallThrowingExpr).getAnExceptionType() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** An `typeid` expression which may throw `std::bad_typeid`. */ |
| 98 | +private class TypeIdThrowingExpr extends TypeidOperator, OriginThrowingExpr { |
| 99 | + override ExceptionType getAnExceptionType() { result instanceof StdBadTypeId } |
| 100 | +} |
| 101 | + |
| 102 | +/** An `new[]` expression which may throw `std::bad_array_new_length`. */ |
| 103 | +private class NewThrowingExpr extends NewArrayExpr, OriginThrowingExpr { |
| 104 | + NewThrowingExpr() { |
| 105 | + // If the extent is known to be below 0 at runtime |
| 106 | + getExtent().getValue().toInt() < 0 |
| 107 | + or |
| 108 | + // initializer has more elements than the array size |
| 109 | + getExtent().getValue().toInt() < getInitializer().(ArrayAggregateLiteral).getArraySize() |
| 110 | + } |
| 111 | + |
| 112 | + override ExceptionType getAnExceptionType() { result instanceof StdBadArrayNewLength } |
| 113 | +} |
0 commit comments