-
Notifications
You must be signed in to change notification settings - Fork 69
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,22 +16,102 @@ | |||||
|
|
||||||
| import cpp | ||||||
| private import ExceptionFlow | ||||||
| private import codingstandards.cpp.Function | ||||||
| private import codingstandards.cpp.Swap | ||||||
| private import codingstandards.cpp.EncapsulatingFunctions | ||||||
| private import codingstandards.cpp.exceptions.ExceptionFlow | ||||||
|
|
||||||
| /** A special function. */ | ||||||
| class SpecialFunction extends Function { | ||||||
| string specialDescription; | ||||||
|
|
||||||
| SpecialFunction() { | ||||||
| this instanceof MoveAssignmentOperator | ||||||
| this instanceof MoveAssignmentOperator and | ||||||
| specialDescription = "move assignment operator" | ||||||
| or | ||||||
| this instanceof MoveConstructor | ||||||
| this instanceof MoveConstructor and | ||||||
| specialDescription = "move constructor" | ||||||
| or | ||||||
| this instanceof Destructor | ||||||
| this instanceof Destructor and | ||||||
| specialDescription = "destructor" | ||||||
| or | ||||||
| this instanceof DeallocationFunction | ||||||
| this instanceof DeallocationFunction and | ||||||
| specialDescription = "deallocation function" | ||||||
| or | ||||||
| this instanceof SwapFunction | ||||||
| this instanceof SwapFunction and | ||||||
| specialDescription = "swap function" | ||||||
| } | ||||||
|
|
||||||
| string getSpecialDescription() { result = specialDescription } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * A function which isn't special by itself, but is used in a special context. | ||||||
| * | ||||||
| * For example, functions which are reachable from initializers of static or thread-local | ||||||
| * variables can result in implementation-defined behavior if they throw exceptions. | ||||||
| */ | ||||||
| abstract class SpecialUseOfFunction extends Expr { | ||||||
| abstract Function getFunction(); | ||||||
|
|
||||||
| abstract string getSpecialDescription(Locatable extra, string extraString); | ||||||
| } | ||||||
|
|
||||||
| class FunctionUsedInInitializer extends FunctionCall, SpecialUseOfFunction { | ||||||
| Variable var; | ||||||
|
|
||||||
| FunctionUsedInInitializer() { | ||||||
| (var.isStatic() or var.isThreadLocal() or var.isTopLevel()) and | ||||||
| exists(Expr initializer | | ||||||
| var.getInitializer().getExpr() = initializer and | ||||||
| getParent*() = initializer | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| override Function getFunction() { result = this.getTarget() } | ||||||
|
|
||||||
| override string getSpecialDescription(Locatable extra, string extraString) { | ||||||
| result = "used to initialize variable $@" and | ||||||
| extra = var and | ||||||
| extraString = var.getName() | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| class FunctionGivenToStdExitHandler extends FunctionAccess, SpecialUseOfFunction { | ||||||
| Function exitHandler; | ||||||
| FunctionCall exitHandlerCall; | ||||||
|
|
||||||
| FunctionGivenToStdExitHandler() { | ||||||
| exitHandler.hasGlobalOrStdName(["atexit", "at_quick_exit", "set_terminate"]) and | ||||||
| exitHandlerCall.getTarget() = exitHandler and | ||||||
| exitHandlerCall.getArgument(0) = this | ||||||
| } | ||||||
|
|
||||||
| override Function getFunction() { result = getTarget() } | ||||||
|
|
||||||
| override string getSpecialDescription(Locatable extra, string extraString) { | ||||||
| result = "$@" and | ||||||
| extra = exitHandlerCall and | ||||||
| extraString = "passed exit handler std::" + exitHandler.getName() | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| class FunctionPassedToExternC extends FunctionAccessLikeExpr, SpecialUseOfFunction { | ||||||
| Function cFunction; | ||||||
| FunctionCall cFunctionCall; | ||||||
|
|
||||||
| FunctionPassedToExternC() { | ||||||
| cFunction.hasCLinkage() and | ||||||
| cFunction = cFunctionCall.getTarget() and | ||||||
| cFunctionCall.getAnArgument() = this | ||||||
| } | ||||||
|
|
||||||
| override Function getFunction() { result = this.(FunctionAccessLikeExpr).getFunction() } | ||||||
|
|
||||||
| override string getSpecialDescription(Locatable extra, string extraString) { | ||||||
| result = "$@ extern \"C\" function '" + cFunction.getName() + "'" and | ||||||
| extra = cFunctionCall and | ||||||
| extraString = "passed to" | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| 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())) | ||
| } | ||
| } |
| 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.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().hasName("main") and | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the rule applies to global
Suggested change
|
||||||
| 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 Function f, UncaughtFunctionCallInMain fc | ||||||
| where | ||||||
| not isExcluded(f, Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery()) and | ||||||
| f.getName() = "main" and | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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" | ||||||
| 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." | ||||||
|
||||||
| select catch, "Catch block catches a class type '" + type + "' by value, which can lead to slicing." | |
| select catch, "Catch block catches a class type '" + type.getQualifiedName() + "' by value, which can lead to slicing." |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.