Skip to content

Commit 1c1f630

Browse files
committed
Add model of external functions that may throw
This allows us to extend the queries reasoning about exceptions that maybe thrown with information that is not directly retrievable from the source. For example, standard library functions known to throw exceptions that are not specified in their signatures.
1 parent 476e910 commit 1c1f630

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

cpp/common/src/codingstandards/cpp/exceptions/ExceptionFlowCustomizations.qll

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
1+
/*
2+
* A library customize models that model the flow of exceptions through the program.
3+
*/
4+
15
import cpp
26
private import codingstandards.cpp.exceptions.ExceptionFlow
37

48
/** A `ThrowingExpr` which is the origin of a exceptions in the program. */
59
abstract class OriginThrowingExpr extends ThrowingExpr { }
610

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+
748
/** An expression which directly throws. */
849
class DirectThrowExprThrowingExpr extends DirectThrowExpr, OriginThrowingExpr {
950
override ExceptionType getAnExceptionType() { result = getExceptionType() }
@@ -46,6 +87,10 @@ class FunctionCallThrowingExpr extends FunctionCall, ThrowingExpr {
4687
isNoExceptTrue(target)
4788
)
4889
)
90+
or
91+
result = this.(ExternalUnderspecifiedFunctionCallThrowingExpr).getAnExceptionType()
92+
or
93+
result = this.(ExternalFunctionCallThrowingExpr).getAnExceptionType()
4994
}
5095
}
5196

cpp/common/src/ext/stdc++.model.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/common-cpp-coding-standards
4+
extensible: throwingFunctionModel
5+
data:
6+
- ["std", "basic_string", "append", "std", "out_of_range"]
7+
- ["std", "basic_string", "reserve", "std", "length_error"]

cpp/common/src/qlpack.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ version: 2.22.0-dev
33
license: MIT
44
dependencies:
55
codeql/cpp-all: 0.9.3
6+
dataExtensions:
7+
- ext/*.model.yml

0 commit comments

Comments
 (0)