Skip to content

Commit 1a9f0a1

Browse files
committed
Fine-tune 21.6.2 to exclude placement-new allocation functions
1 parent ccd0945 commit 1a9f0a1

File tree

2 files changed

+110
-43
lines changed

2 files changed

+110
-43
lines changed

cpp/misra/src/rules/RULE-21-6-2/DynamicMemoryManagedManually.ql

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,29 @@ import cpp
1515
import codingstandards.cpp.misra
1616
import codingstandards.cpp.SmartPointers
1717

18+
/**
19+
* An `operator new` or `operator new[]` allocation function called by a placement-new expression.
20+
*
21+
* The operator functions are supposed to have a `std::size_t` as their first parameter and a
22+
* `void*` parameter somewhere in the rest of the parameter list.
23+
*/
24+
class PlacementNewOrNewArrayAllocationFunction extends AllocationFunction {
25+
PlacementNewOrNewArrayAllocationFunction() {
26+
this.getName() in ["operator new", "operator new[]"] and
27+
this.getParameter(0).getType().resolveTypedefs*() instanceof Size_t and
28+
this.getAParameter().getUnderlyingType() instanceof VoidPointerType
29+
}
30+
}
31+
1832
class DynamicMemoryManagementFunction extends Function {
1933
string description;
2034

2135
DynamicMemoryManagementFunction() {
22-
(this instanceof AllocationFunction or this instanceof AlignedAlloc) and
36+
(
37+
(this instanceof AllocationFunction or this instanceof AlignedAlloc) and
38+
/* Placement-new expressions are not prohibited by this rule, but by Rule 21.6.3. */
39+
not this instanceof PlacementNewOrNewArrayAllocationFunction
40+
) and
2341
description = "an expression that dynamically allocates memory"
2442
or
2543
this instanceof DeallocationFunction and
@@ -56,6 +74,9 @@ class AlignedAlloc extends Function {
5674
AlignedAlloc() { this.hasGlobalOrStdName("aligned_alloc") }
5775
}
5876

77+
/**
78+
* The `std::unique_ptr::release` member function.
79+
*/
5980
class UniquePointerReleaseFunction extends MemberFunction {
6081
UniquePointerReleaseFunction() { this.getClassAndName("release") instanceof AutosarUniquePointer }
6182
}

0 commit comments

Comments
 (0)