Conversation
also adjust formatting on prev changenote which was incorrect
MichaelRFairhurst
left a comment
There was a problem hiding this comment.
The test cases look great Kristen, thank you!
| AllocatedObjectIdentity() { | ||
| this.(FunctionCall).getTarget().(AllocationFunction).requiresDealloc() | ||
| or | ||
| this = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer())) |
There was a problem hiding this comment.
Pull request overview
This PR adds a new "Lifetime" rule package for MISRA C++ 2023, implementing two new rules: RULE-11-6-2 (reading uninitialized objects) and RULE-6-8-3 (automatic storage assigned to wider lifetime). It also enhances the shared ReadOfUninitializedMemory query to detect uninitialized reads through new expressions without value initialization, and extends the AllocatedObjectIdentity class to recognize new/new[] expressions as allocated storage.
Changes:
- Adds the Lifetime rule package with RULE-11-6-2 and RULE-6-8-3, including exclusion metadata, query files, test references, and supporting infrastructure.
- Enhances the
ReadOfUninitializedMemoryshared library to detect uninitialized memory accessed vianewexpressions (without value initialization), incorporating a copiedInitializationFunctions.qllfrom the CodeQL standard library. - Extends
AllocatedObjectIdentityinCppObjects.qllto includenew/new[]expressions (excluding placement new) for storage duration tracking.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
rules.csv |
Updates RULE-11-6-2 to reference the Lifetime package |
rule_packages/cpp/Lifetime.json |
New rule package definition for RULE-11-6-2 and RULE-6-8-3 |
cpp/common/src/codingstandards/cpp/exclusions/cpp/Lifetime.qll |
Autogenerated exclusion module for Lifetime package |
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll |
Integrates Lifetime package into rule metadata |
cpp/misra/src/rules/RULE-11-6-2/ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet.ql |
New query for RULE-11-6-2 using shared ReadOfUninitializedMemory |
cpp/misra/src/rules/RULE-6-8-3/AutomaticStorageAssignedToObjectGreaterLifetime.ql |
New query for RULE-6-8-3 using shared DoNotCopyAddressOfAutoStorage |
cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/ReadOfUninitializedMemory.qll |
Enhanced to handle new without value init |
cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/InitializationFunctions.qll |
Copied from CodeQL standard library for init function tracking |
cpp/common/src/codingstandards/cpp/lifetimes/CppObjects.qll |
Extended AllocatedObjectIdentity for new/new[] expressions |
cpp/common/test/rules/readofuninitializedmemory/test.cpp |
Added test cases for new uninitialized memory scenarios |
cpp/common/test/rules/readofuninitializedmemory/ReadOfUninitializedMemory.expected |
Updated expected results for new test cases |
cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/test.cpp |
Added test cases for auto-storage address escape |
cpp/common/test/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.expected |
Updated expected results for new test cases |
cpp/misra/test/rules/RULE-6-8-3/AutomaticStorageAssignedToObjectGreaterLifetime.testref |
Test reference for RULE-6-8-3 |
cpp/misra/test/rules/RULE-11-6-2/ValueOfAnObjectMustNotBeReadBeforeItHasBeenSet.testref |
Test reference for RULE-11-6-2 |
change_notes/2026-02-03-uninitialized-mem-improve.md |
Change note for shared query modifications |
.vscode/tasks.json |
Added Lifetime to VS Code task configuration |
Comments suppressed due to low confidence (1)
cpp/common/src/codingstandards/cpp/rules/readofuninitializedmemory/ReadOfUninitializedMemory.qll:207
- Typo in docstring: "Get a access" should be "Gets an access".
* Get a access of the variable that is assumed to initialize the variable.
| module AutomaticStorageAssignedToObjectGreaterLifetimeConfig implements | ||
| DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig | ||
| { | ||
| Query getQuery() { | ||
| result = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery() | ||
| } | ||
| } | ||
|
|
||
| import DoNotCopyAddressOfAutoStorageObjectToOtherObject<AutomaticStorageAssignedToObjectGreaterLifetimeConfig> |
There was a problem hiding this comment.
The query references DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig (line 21) and uses DoNotCopyAddressOfAutoStorageObjectToOtherObject<...> as a parameterized module (line 25), but the shared library at cpp/common/src/codingstandards/cpp/rules/donotcopyaddressofautostorageobjecttootherobject/DoNotCopyAddressOfAutoStorageObjectToOtherObject.qll does not define any ConfigSig signature module or parameterized module. It only uses the abstract class pattern (like M7-5-2/AssignmentOfEscapingAutoStorage.ql does). This will fail to compile. The query should either use the existing abstract class pattern (extending DoNotCopyAddressOfAutoStorageObjectToOtherObjectSharedQuery), or the .qll file needs to be refactored to support the parameterized module pattern.
| module AutomaticStorageAssignedToObjectGreaterLifetimeConfig implements | |
| DoNotCopyAddressOfAutoStorageObjectToOtherObjectConfigSig | |
| { | |
| Query getQuery() { | |
| result = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery() | |
| } | |
| } | |
| import DoNotCopyAddressOfAutoStorageObjectToOtherObject<AutomaticStorageAssignedToObjectGreaterLifetimeConfig> | |
| /** | |
| * Concrete query configuration for RULE-6-8-3 using the shared | |
| * DoNotCopyAddressOfAutoStorageObjectToOtherObject infrastructure. | |
| */ | |
| class AutomaticStorageAssignedToObjectGreaterLifetimeQuery | |
| extends DoNotCopyAddressOfAutoStorageObjectToOtherObjectSharedQuery | |
| { | |
| AutomaticStorageAssignedToObjectGreaterLifetimeQuery() { | |
| this = LifetimePackage::automaticStorageAssignedToObjectGreaterLifetimeQuery() | |
| } | |
| } |
| cpp,MISRA-C++-2023,RULE-11-3-2,Yes,Advisory,Decidable,Single Translation Unit,The declaration of an object should contain no more than two levels of pointer indirection,A5-0-3,ImportMisra23,Import, | ||
| cpp,MISRA-C++-2023,RULE-11-6-1,Yes,Advisory,Decidable,Single Translation Unit,All variables should be initialized,,Declarations2,Easy, | ||
| cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Very Hard, | ||
| cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import |
There was a problem hiding this comment.
The RULE-11-6-2 row is missing a trailing comma after Import. All other rows in the CSV consistently end with a comma after the difficulty field (e.g., line 929: ...,Import,). This could cause parsing issues with downstream tools that process the CSV.
| cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import | |
| cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import, |
| int *p3 = new int(1); | ||
| *p3 = *p2; // NON_COMPLIANT -- the pointee of p2 has not been | ||
| // initialized | ||
| *p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden |
There was a problem hiding this comment.
Typo in comment: "has be overridden" should be "has been overridden".
| *p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has be overridden | |
| *p3; // NON_COMPLIANT[FALSE_NEGATIVE] -- the pointee of p3 has been overridden |
|
|
||
| int *i3; | ||
|
|
||
| if (i3 == i1) { // NON_COMPLIANT |
There was a problem hiding this comment.
Line 141 is annotated with a single // NON_COMPLIANT comment but produces two alerts in the expected file (lines 8-9): one for i3 (a genuinely uninitialized pointer) and one for i1 (initialized with new int — the pointer value itself is valid, only the pointee is uninitialized). The second alert for i1 in a pointer comparison i3 == i1 appears to be a false positive — comparing a pointer initialized via new int doesn't read uninitialized memory. The annotation should be updated to reflect this, for example by marking one result as NON_COMPLIANT and the other as COMPLIANT[FALSE_POSITIVE], or by adding a comment explaining the two alerts.
|
|
||
| /** Get a read of the variable. */ | ||
| /** | ||
| * Gets an access of the variable `v` which is not used as an lvalue, and not used as an argument |
There was a problem hiding this comment.
The docstring references "the variable v" but there is no parameter v in this method. Since this is a method on UninitializedVariable, it should reference "this variable" or just "the variable" instead.
This issue also appears on line 207 of the same file.
| * Gets an access of the variable `v` which is not used as an lvalue, and not used as an argument | |
| * Gets an access of this variable which is not used as an lvalue, and not used as an argument |
| or | ||
| this = any(NewOrNewArrayExpr new | not exists(new.getPlacementPointer())) |
There was a problem hiding this comment.
The class docstring says "An object identified by a call to malloc" but now also includes new/new[] expressions (excluding placement new). The documentation should be updated to reflect the expanded scope, e.g. "An object identified by a dynamic allocation expression (malloc, new, new[])."
we will need to change these cases in batches
Description
add rules for lifetime package, two shared rules, one with improvements upon previous, one as simple shared
Change request type
.ql,.qll,.qlsor unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
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.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
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
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.
Reviewer
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.