Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MichaelRFairhurst
Copy link
Contributor

Description

Misra C++ exceptions rules

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • RULE-18-3-1
    • RULE-18-3-2
    • RULE-18-4-1
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

Query development review checklist

For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:

Author

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Adds support for:
- RULE-18-3-1
- RULE-18-3-2
- RULE-18-4-1
@Copilot Copilot AI review requested due to automatic review settings May 20, 2025 17:18
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new “Exceptions3” package for MISRA C++ exception rules, adding three new rules (18-3-1, 18-3-2, 18-4-1) along with tests, query implementations, and metadata updates.

  • Added unit tests (.cpp, .qlref, .expected) covering four variants of rule 18-3-1.
  • Introduced CodeQL queries for RULE-18-3-1 (missing catch-all in main), RULE-18-3-2 (class exceptions caught by value), and RULE-18-4-1 (exception-unfriendly functions must be noexcept).
  • Updated exclusion metadata (RuleMetadata.qll, Exceptions3.qll) and supporting QLL libraries (ExceptionFlow, Function, SpecialFunctionExceptions).

Reviewed Changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
cpp/misra/test/rules/RULE-18-3-1.4/test_except_inside_catch.cpp Add NON-COMPLIANT test where cout in catch-all may throw
cpp/misra/test/rules/RULE-18-3-1.4/MissingCatchAllExceptionHandlerInMain.qlref Reference the new 18-3-1.4 test for the missing-catch rule
cpp/misra/test/rules/RULE-18-3-1.4/MissingCatchAllExceptionHandlerInMain.expected Add expected alert for test_except_inside_catch.cpp
cpp/misra/test/rules/RULE-18-3-1.3/test_except_after_catch.cpp Add NON-COMPLIANT test where f() after catch may throw
cpp/misra/test/rules/RULE-18-3-1.3/MissingCatchAllExceptionHandlerInMain.qlref Reference the new 18-3-1.3 test for the missing-catch rule
cpp/misra/test/rules/RULE-18-3-1.3/MissingCatchAllExceptionHandlerInMain.expected Add expected alert for test_except_after_catch.cpp
cpp/misra/test/rules/RULE-18-3-1.2/test_except_before_catch.cpp Add NON-COMPLIANT test where f() before catch may throw
cpp/misra/test/rules/RULE-18-3-1.2/MissingCatchAllExceptionHandlerInMain.qlref Reference the new 18-3-1.2 test for the missing-catch rule
cpp/misra/test/rules/RULE-18-3-1.2/MissingCatchAllExceptionHandlerInMain.expected Add expected alert for test_except_before_catch.cpp
cpp/misra/test/rules/RULE-18-3-1.1/test_missing.cpp Add NON-COMPLIANT test with no catch-all handler
cpp/misra/test/rules/RULE-18-3-1.1/MissingCatchAllExceptionHandlerInMain.qlref Reference the new 18-3-1.1 test for the missing-catch rule
cpp/misra/test/rules/RULE-18-3-1.1/MissingCatchAllExceptionHandlerInMain.expected Add expected alert for test_missing.cpp
cpp/misra/src/rules/RULE-18-4-1/ExceptionUnfriendlyFunctionMustBeNoexcept.ql Implement query for exception-unfriendly functions needing noexcept
cpp/misra/src/rules/RULE-18-3-2/ClassExceptionCaughtByValue.ql Implement query to enforce catching classes by (const) reference
cpp/misra/src/rules/RULE-18-3-1/MissingCatchAllExceptionHandlerInMain.ql Implement catch-all-in-main query with custom predicate
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Import new Exceptions3 package in exclusion metadata
cpp/common/src/codingstandards/cpp/exclusions/cpp/Exceptions3.qll Auto-generate Exceptions3Query type and metadata checks
cpp/common/src/codingstandards/cpp/exceptions/ExceptionFlow.qll Add helper getATryStmt to locate enclosing try blocks
cpp/common/src/codingstandards/cpp/exceptions/Function.qll Add UserDeclaredFunction and function-access-like classes
cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll Extend special-function exception descriptions and contexts
Comments suppressed due to low confidence (2)

cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll:18

  • [nitpick] Remove or consolidate the redundant import of ExceptionFlow, since it’s also imported with a fully-qualified path.
private import ExceptionFlow

cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll:107

  • [nitpick] Adjust indentation of the closing brace to align with the opening brace for consistency.
   }

/**
* @id cpp/misra/exception-unfriendly-function-must-be-noexcept
* @name RULE-18-4-1: Exception-unfriendly functions shall be noexcept
* @description Throwing exceptions in constructors, descructors, copy-constructors, move
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix typo: change 'descructors' to 'destructors' in the rule description.

Suggested change
* @description Throwing exceptions in constructors, descructors, copy-constructors, move
* @description Throwing exceptions in constructors, destructors, copy-constructors, move

Copilot uses AI. Check for mistakes.

@@ -121,6 +121,11 @@ TryStmt getNearestTry(Stmt s) {
else result = getNearestTry(s.getParent())
}

/** Gets a try statement that contains the given statement. */
TryStmt getATryStmt(Stmt s) {
result.getStmt() = s.getEnclosingStmt().getEnclosingBlock*()
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getATryStmt predicate may match any enclosing block rather than specifically a TryStmt. Consider modeling it after getNearestTry to ensure it only returns actual TryStmt nodes.

Suggested change
result.getStmt() = s.getEnclosingStmt().getEnclosingBlock*()
result = s.getEnclosingStmt().getEnclosingBlock*() and result instanceof TryStmt

Copilot uses AI. Check for mistakes.

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."
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] When concatenating the type in the select message, use type.toString() or type.getQualifiedName() to ensure a clear textual representation.

Suggested change
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."

Copilot uses AI. Check for mistakes.

@MichaelRFairhurst MichaelRFairhurst requested a review from lcartey May 20, 2025 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant