-
Notifications
You must be signed in to change notification settings - Fork 70
Implement package exceptions3 for MISRA C++ #901
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
MichaelRFairhurst
merged 6 commits into
main
from
michaelrfairhurst/implement-package-exceptions3
Dec 9, 2025
+800
−5
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc00564
Implement package exceptions3 for MISRA C++
MichaelRFairhurst a329da0
Format missed file
MichaelRFairhurst 4cc50dd
Format missed file ExceptionFlow.qll
MichaelRFairhurst 3dc5aa4
Address feedback
MichaelRFairhurst 98dd496
Merge remote-tracking branch 'origin/main' into HEAD
MichaelRFairhurst 11a3418
Fix test expectations
MichaelRFairhurst 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
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
61 changes: 61 additions & 0 deletions
61
cpp/common/src/codingstandards/cpp/exclusions/cpp/Exceptions3.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,61 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype Exceptions3Query = | ||
| TMissingCatchAllExceptionHandlerInMainQuery() or | ||
| TClassExceptionCaughtByValueQuery() or | ||
| TExceptionUnfriendlyFunctionMustBeNoexceptQuery() | ||
|
|
||
| predicate isExceptions3QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `missingCatchAllExceptionHandlerInMain` query | ||
| Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery() and | ||
| queryId = | ||
| // `@id` for the `missingCatchAllExceptionHandlerInMain` query | ||
| "cpp/misra/missing-catch-all-exception-handler-in-main" and | ||
| ruleId = "RULE-18-3-1" and | ||
| category = "advisory" | ||
| or | ||
| query = | ||
| // `Query` instance for the `classExceptionCaughtByValue` query | ||
| Exceptions3Package::classExceptionCaughtByValueQuery() and | ||
| queryId = | ||
| // `@id` for the `classExceptionCaughtByValue` query | ||
| "cpp/misra/class-exception-caught-by-value" and | ||
| ruleId = "RULE-18-3-2" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
| Exceptions3Package::exceptionUnfriendlyFunctionMustBeNoexceptQuery() and | ||
| queryId = | ||
| // `@id` for the `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
| "cpp/misra/exception-unfriendly-function-must-be-noexcept" and | ||
| ruleId = "RULE-18-4-1" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module Exceptions3Package { | ||
| Query missingCatchAllExceptionHandlerInMainQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `missingCatchAllExceptionHandlerInMain` query | ||
| TQueryCPP(TExceptions3PackageQuery(TMissingCatchAllExceptionHandlerInMainQuery())) | ||
| } | ||
|
|
||
| Query classExceptionCaughtByValueQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `classExceptionCaughtByValue` query | ||
| TQueryCPP(TExceptions3PackageQuery(TClassExceptionCaughtByValueQuery())) | ||
| } | ||
|
|
||
| Query exceptionUnfriendlyFunctionMustBeNoexceptQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
| TQueryCPP(TExceptions3PackageQuery(TExceptionUnfriendlyFunctionMustBeNoexceptQuery())) | ||
| } | ||
| } |
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
56 changes: 56 additions & 0 deletions
56
cpp/misra/src/rules/RULE-18-3-1/MissingCatchAllExceptionHandlerInMain.ql
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,56 @@ | ||
| /** | ||
| * @id cpp/misra/missing-catch-all-exception-handler-in-main | ||
| * @name RULE-18-3-1: There should be at least one exception handler to catch all otherwise unhandled exceptions | ||
| * @description The main function should have a catch-all exception handler (catch(...)) to catch | ||
| * all otherwise unhandled exceptions. | ||
| * @kind problem | ||
| * @precision high | ||
| * @problem.severity warning | ||
| * @tags external/misra/id/rule-18-3-1 | ||
| * scope/single-translation-unit | ||
| * maintainability | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.EncapsulatingFunctions | ||
| import codingstandards.cpp.exceptions.ExceptionFlow | ||
| import codingstandards.cpp.exceptions.ExceptionSpecifications | ||
|
|
||
| /** | ||
| * A function call in main that is not declared noexcept and is not within a catch-all | ||
| * exception handler (catch(...)). | ||
| */ | ||
| class UncaughtFunctionCallInMain extends FunctionCall { | ||
| UncaughtFunctionCallInMain() { | ||
| getEnclosingFunction() instanceof MainFunction and | ||
| not isNoExceptTrue(getTarget()) and | ||
| not exists(TryStmt try | | ||
| try = getATryStmt(this.getEnclosingStmt()) and | ||
| try.getCatchClause(_) instanceof CatchAnyBlock | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * We only want to report one counter-example indicating a missing catch(...), so this holds only | ||
| * for the first one we find. | ||
| */ | ||
| predicate isFirst() { | ||
| this = | ||
| rank[1](UncaughtFunctionCallInMain fc | | ||
| any() | ||
| | | ||
| fc order by fc.getLocation().getStartLine(), fc.getLocation().getStartColumn() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| from MainFunction f, UncaughtFunctionCallInMain fc | ||
| where | ||
| not isExcluded(f, Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery()) and | ||
| fc.isFirst() | ||
| select f, | ||
| "Main function has a $@ which is not within a try block with a catch-all ('catch(...)') handler.", | ||
| fc, "function call that may throw" |
25 changes: 25 additions & 0 deletions
25
cpp/misra/src/rules/RULE-18-3-2/ClassExceptionCaughtByValue.ql
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,25 @@ | ||
| /** | ||
| * @id cpp/misra/class-exception-caught-by-value | ||
| * @name RULE-18-3-2: An exception of class type shall be caught by const reference or reference | ||
| * @description Catching exception classes by value can lead to slicing, which can result in | ||
| * unexpected behavior. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-18-3-2 | ||
| * scope/single-translation-unit | ||
| * correctness | ||
| * maintainability | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| from CatchBlock catch, Type type | ||
| where | ||
| not isExcluded(catch, Exceptions3Package::classExceptionCaughtByValueQuery()) and | ||
| type = catch.getParameter().getType() and | ||
| type.stripTopLevelSpecifiers() instanceof Class | ||
| select catch, "Catch block catches a class type '" + type + "' by value, which can lead to slicing." | ||
Oops, something went wrong.
Oops, something went wrong.
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.