-
Notifications
You must be signed in to change notification settings - Fork 66
Fix FP reported in 424 #532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d23d3ed
Extract customizations into own module
rvermeulen 476e910
Add test case of FP
rvermeulen 1c1f630
Add model of external functions that may throw
rvermeulen 3413966
Add changenote
rvermeulen b3ff452
Add test case annotation
rvermeulen 1d5a2a8
Merge branch 'main' into rvermeulen/fix-424
rvermeulen 0f0170a
Merge branch 'main' into rvermeulen/fix-424
knewbury01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-`A15-4-4` - `MissingNoExcept.ql`: | ||
- Fix FP reported in #424. Exclude functions calling `std::string::reserve` or `std::string::append` that may throw even if their signatures don't specify it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
cpp/common/src/codingstandards/cpp/exceptions/ExceptionFlowCustomizations.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* A library customize models that model the flow of exceptions through the program. | ||
*/ | ||
|
||
import cpp | ||
private import codingstandards.cpp.exceptions.ExceptionFlow | ||
|
||
/** A `ThrowingExpr` which is the origin of a exceptions in the program. */ | ||
abstract class OriginThrowingExpr extends ThrowingExpr { } | ||
|
||
/** | ||
* A `FunctionCall` to an external function without an exception specification that * | ||
* may throw an exception. | ||
*/ | ||
abstract class ExternalUnderspecifiedFunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { } | ||
|
||
/** | ||
* An extensible predicate that describes functions that when called may throw an exception. | ||
*/ | ||
extensible predicate throwingFunctionModel( | ||
string functionNamespaceQualifier, string functionTypeQualifier, string functionName, | ||
string exceptionNamespaceQualifier, string exceptionType | ||
); | ||
|
||
/** | ||
* A `FunctionCall` that may throw an exception of type `ExceptionType` as provded by | ||
* the extensible predicate `throwingFunctionModel`. | ||
*/ | ||
private class ExternalFunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { | ||
ExceptionType exceptionType; | ||
|
||
ExternalFunctionCallThrowingExpr() { | ||
exists( | ||
string functionNamespaceQualifier, string functionTypeQualifier, string functionName, | ||
string exceptionNamespaceQualifier, string exceptionTypeSpec | ||
| | ||
throwingFunctionModel(functionNamespaceQualifier, functionTypeQualifier, functionName, | ||
exceptionNamespaceQualifier, exceptionTypeSpec) and | ||
this.getTarget() | ||
.hasQualifiedName(functionNamespaceQualifier, functionTypeQualifier, functionName) and | ||
exceptionType.(Class).hasQualifiedName(exceptionNamespaceQualifier, exceptionTypeSpec) | ||
) | ||
} | ||
|
||
override ExceptionType getAnExceptionType() { result = exceptionType } | ||
} | ||
|
||
/** An expression which directly throws. */ | ||
class DirectThrowExprThrowingExpr extends DirectThrowExpr, OriginThrowingExpr { | ||
override ExceptionType getAnExceptionType() { result = getExceptionType() } | ||
} | ||
|
||
/** A `ReThrowExpr` which throws a previously caught exception. */ | ||
class ReThrowExprThrowingExpr extends ReThrowExpr, ThrowingExpr { | ||
predicate rethrows(CatchBlock cb, ExceptionType et, ThrowingExpr te) { | ||
// Find the nearest CatchBlock | ||
cb = getNearestCatch(this.getEnclosingStmt()) and | ||
// Find an `ExceptionType` which is caught by this catch block, and `ThrowingExpr` which throws that exception type | ||
catches(cb, te, et) | ||
} | ||
|
||
override ExceptionType getAnExceptionType() { rethrows(_, result, _) } | ||
|
||
CatchBlock getCatchBlock() { rethrows(result, _, _) } | ||
} | ||
|
||
/** An expression which calls a function which may throw an exception. */ | ||
class FunctionCallThrowingExpr extends FunctionCall, ThrowingExpr { | ||
override ExceptionType getAnExceptionType() { | ||
exists(Function target | | ||
target = getTarget() and | ||
result = getAFunctionThrownType(target, _) and | ||
// [expect.spec] states that throwing an exception type that is prohibited | ||
// by the specification will result in the program terminating, unless | ||
// a custom `unexpected_handler` is registered that throws an exception type | ||
// which is compatible with the dynamic exception specification, or the | ||
// dynamic exception specification lists `std::bad_exception`, in which case | ||
// a `std::bad_exception` is thrown. | ||
// As dynamic exception specifications and the `unexpected_handler` are both | ||
// deprecated in C++14 and removed in C++17, we assume a default | ||
// `std::unexpected` handler that calls `std::terminate` and therefore | ||
// do not propagate such exceptions to the call sites for the function. | ||
not ( | ||
hasDynamicExceptionSpecification(target) and | ||
not result = getAHandledExceptionType(target.getAThrownType()) | ||
or | ||
isNoExceptTrue(target) | ||
) | ||
) | ||
or | ||
result = this.(ExternalUnderspecifiedFunctionCallThrowingExpr).getAnExceptionType() | ||
or | ||
result = this.(ExternalFunctionCallThrowingExpr).getAnExceptionType() | ||
} | ||
} | ||
|
||
/** An `typeid` expression which may throw `std::bad_typeid`. */ | ||
private class TypeIdThrowingExpr extends TypeidOperator, OriginThrowingExpr { | ||
override ExceptionType getAnExceptionType() { result instanceof StdBadTypeId } | ||
} | ||
|
||
/** An `new[]` expression which may throw `std::bad_array_new_length`. */ | ||
private class NewThrowingExpr extends NewArrayExpr, OriginThrowingExpr { | ||
NewThrowingExpr() { | ||
// If the extent is known to be below 0 at runtime | ||
getExtent().getValue().toInt() < 0 | ||
or | ||
// initializer has more elements than the array size | ||
getExtent().getValue().toInt() < getInitializer().(ArrayAggregateLiteral).getArraySize() | ||
} | ||
|
||
override ExceptionType getAnExceptionType() { result instanceof StdBadArrayNewLength } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extensions: | ||
- addsTo: | ||
pack: codeql/common-cpp-coding-standards | ||
extensible: throwingFunctionModel | ||
data: | ||
- ["std", "basic_string", "append", "std", "out_of_range"] | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- ["std", "basic_string", "reserve", "std", "length_error"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ version: 2.22.0-dev | |
license: MIT | ||
dependencies: | ||
codeql/cpp-all: 0.9.3 | ||
dataExtensions: | ||
- ext/*.model.yml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.