Skip to content

Commit 6bbd610

Browse files
committed
Rename to PlacementNewOperator, move it, and add some docstrings
1 parent 568daa7 commit 6bbd610

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

cpp/common/src/codingstandards/cpp/DynamicMemory.qll

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22

33
import cpp
44

5-
/**
6-
* An `operator new` or `operator new[]` allocation function called by a placement-new expression.
7-
*
8-
* The operator functions have a `std::size_t` as their first parameter and a
9-
* `void*` parameter somewhere in the rest of the parameter list.
10-
*/
11-
class PlacementNewOrNewArrayAllocationFunction extends AllocationFunction {
12-
PlacementNewOrNewArrayAllocationFunction() {
13-
this.getName() in ["operator new", "operator new[]"] and
14-
this.getParameter(0).getType().resolveTypedefs*() instanceof Size_t and
15-
this.getAParameter().getUnderlyingType() instanceof VoidPointerType
16-
}
17-
}
18-
195
/**
206
* A function that has namespace `std` and has name `allocate` or `deallocate`, including but
217
* not limited to:

cpp/common/src/codingstandards/cpp/allocations/CustomOperatorNewDelete.qll

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Provides classes to help reasoning about `operator new`, `operator new[]`,
3+
* `operator delete`, and `operator delete[]`.
4+
*
5+
* These are described in section [support.dynamic] of the C++ standard.
6+
*/
7+
18
import cpp
29
import codingstandards.cpp.Handlers
310

@@ -21,6 +28,13 @@ abstract class OperatorNewOrDelete extends Operator {
2128
}
2229
}
2330

31+
/**
32+
* An `operator new` and `operator new[]` function described in [new.delete.single]
33+
* and [new.delete.array], respectively.
34+
*
35+
* Note that these do not include [new.delete.placement]. These are captured in
36+
* `PlacementOperatorNew`.
37+
*/
2438
class ReplaceableOperatorNew extends OperatorNewOrDelete {
2539
ReplaceableOperatorNew() {
2640
this.getName().regexpMatch("operator new(\\[\\])?") and
@@ -34,6 +48,10 @@ class ReplaceableOperatorNew extends OperatorNewOrDelete {
3448
}
3549
}
3650

51+
/**
52+
* `operator new`, `operator new[]`, `operator delete`, or `operator delete[]` functions
53+
* that are very likely provided by the user.
54+
*/
3755
class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
3856
CustomOperatorNewOrDelete() {
3957
this.hasDefinition() and
@@ -62,6 +80,10 @@ class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
6280

6381
class CustomReplaceableOperatorNew extends CustomOperatorNewOrDelete, ReplaceableOperatorNew { }
6482

83+
/**
84+
* An `operator delete` or `operator delete[]` deallocation function described in
85+
* [new.delete.single] and [new.delete.array], respectively.
86+
*/
6587
class ReplaceableOperatorDelete extends OperatorNewOrDelete {
6688
ReplaceableOperatorDelete() {
6789
this.getName().regexpMatch("operator delete(\\[\\])?") and
@@ -95,3 +117,18 @@ class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, Replace
95117
else result.getPartner() = this
96118
}
97119
}
120+
121+
/**
122+
* An `operator new` or `operator new[]` allocation function called by a placement-new expression,
123+
* as described in [new.delete.placement].
124+
*
125+
* The operator functions have a `std::size_t` as their first parameter and a
126+
* `void*` parameter somewhere in the rest of the parameter list.
127+
*/
128+
class PlacementOperatorNew extends AllocationFunction {
129+
PlacementOperatorNew() {
130+
this.getName() in ["operator new", "operator new[]"] and
131+
this.getParameter(0).getType().resolveTypedefs*() instanceof Size_t and
132+
this.getAParameter().getUnderlyingType() instanceof VoidPointerType
133+
}
134+
}

cpp/misra/src/rules/RULE-21-6-1/DynamicMemoryShouldNotBeUsed.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import cpp
1717
import codingstandards.cpp.misra
1818
import codingstandards.cpp.DynamicMemory
19+
import codingstandards.cpp.allocations.CustomOperatorNewDelete
1920

2021
/**
2122
* A function that directly or indirectly allocates dynamic memory.
@@ -32,7 +33,7 @@ abstract class DynamicMemoryAllocatingFunction extends Function { }
3233
class DirectDynamicMemoryAllocatingFunction extends DynamicMemoryAllocatingFunction {
3334
DirectDynamicMemoryAllocatingFunction() {
3435
this instanceof AllocationFunction and
35-
not this instanceof PlacementNewOrNewArrayAllocationFunction
36+
not this instanceof PlacementOperatorNew
3637
or
3738
this.hasGlobalOrStdName("aligned_alloc")
3839
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import cpp
1818
import codingstandards.cpp.misra
1919
import codingstandards.cpp.SmartPointers
2020
import codingstandards.cpp.DynamicMemory
21+
import codingstandards.cpp.allocations.CustomOperatorNewDelete
2122

2223
class DynamicMemoryManagementFunction extends Function {
2324
DynamicMemoryManagementFunction() {
2425
(this instanceof AllocationFunction or this instanceof AlignedAllocFunction) and
2526
/* Avoid duplicate alerts on `realloc` which is both an `AllocationFunction` and a `DeallocationFunction`. */
2627
not this instanceof ReallocFunction and
2728
/* Placement-new expressions are not prohibited by this rule, but by Rule 21.6.3. */
28-
not this instanceof PlacementNewOrNewArrayAllocationFunction
29+
not this instanceof PlacementOperatorNew
2930
or
3031
this instanceof DeallocationFunction and
3132
/* Avoid duplicate alerts on `realloc` which is both an `AllocationFunction` and a `DeallocationFunction`. */

0 commit comments

Comments
 (0)