Skip to content

Commit 81d9829

Browse files
committed
Finalize draft and update expected results of the query
1 parent 3089c6f commit 81d9829

File tree

2 files changed

+181
-131
lines changed

2 files changed

+181
-131
lines changed

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

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ abstract class DynamicMemoryAllocatingFunction extends Function { }
2525

2626
/**
2727
* A function that directly allocates dynamic memory.
28-
* Includes C allocation functions (malloc, calloc, realloc, aligned_alloc)
29-
* and C++ allocation functions (operator new, operator new[]).
28+
* Includes C allocation functions (`malloc`, `calloc`, realloc`,` `aligned_alloc`)
29+
* and C++ allocation functions (`operator new`, `operator new[]`).
3030
*
3131
* This excludes placement-new operators, as they do not allocate memory themselves.
3232
*/
@@ -48,38 +48,29 @@ class DirectDynamicMemoryAllocatingFunction extends DynamicMemoryAllocatingFunct
4848
abstract class IndirectDynamicMemoryAllocatingFunction extends DynamicMemoryAllocatingFunction { }
4949

5050
/**
51-
* A constructor of a standard library container that uses `std::allocator` directly
51+
* A constructor of a standard library classes that uses `std::allocator` directly
5252
* as template argument or under the hood as the default value of the template argument.
53-
* Includes `vector`, `deque`, `list`, `forward_list`, `set`, `map`, `multiset`, `multimap`,
54-
* `unordered_set`, `unordered_map`, `unordered_multiset`, `unordered_multimap`, and `valarray`.
55-
*/
56-
class AllocatorContainerConstructor extends IndirectDynamicMemoryAllocatingFunction {
57-
AllocatorContainerConstructor() {
58-
this instanceof Constructor and
59-
this.getDeclaringType()
60-
.hasQualifiedName("std",
61-
[
62-
"vector", "deque", "list", "forward_list", "set", "map", "multiset", "multimap",
63-
"unordered_set", "unordered_map", "unordered_multiset", "unordered_multimap", "valarray"
64-
])
65-
}
66-
}
67-
68-
/**
69-
* A constructor of a standard library string type that uses std::allocator.
70-
* Includes basic_string and its aliases (string, wstring, u16string, u32string).
53+
* This class can be divided into big categories:
54+
*
55+
* 1. A constructor of standard library containers such as `vector`, `deque`, `unordered_set`.
56+
* 2. A constructor of standard library strings such as `string`, `wstring` that derives from
57+
* `std::basic_string`.
7158
*/
72-
class AllocatorStringConstructor extends IndirectDynamicMemoryAllocatingFunction {
73-
AllocatorStringConstructor() {
74-
this instanceof Constructor and
59+
class AllocatorContructor extends IndirectDynamicMemoryAllocatingFunction {
60+
AllocatorContructor() {
61+
/* Ensure that the constructor accepts a `std::allocator`. */
7562
this.getDeclaringType()
76-
.hasQualifiedName("std", ["basic_string", "string", "wstring", "u16string", "u32string"])
63+
.(ClassTemplateInstantiation)
64+
.getATemplateArgument()
65+
.(Type)
66+
.resolveTypedefs() instanceof StdAllocator
7767
}
7868
}
7969

8070
/**
8171
* A constructor of a container adaptor that contains an allocating container by default.
82-
* Includes stack (contains deque), queue (contains deque), and priority_queue (contains vector).
72+
* Includes `stack` (contains `deque`), `queue` (contains `deque`), and `priority_queue`
73+
* (contains `vector`).
8374
*/
8475
class ContainerAdaptorConstructor extends IndirectDynamicMemoryAllocatingFunction {
8576
ContainerAdaptorConstructor() {
@@ -89,7 +80,7 @@ class ContainerAdaptorConstructor extends IndirectDynamicMemoryAllocatingFunctio
8980
}
9081

9182
/**
92-
* A constructor of a string stream that contains std::basic_string for buffer storage.
83+
* A constructor of a string stream that contains `std::basic_string` for buffer storage.
9384
* Includes `basic_stringstream`, `stringstream`, `wstringstream`,
9485
* `basic_istringstream`, `istringstream`, `wistringstream`,
9586
* `basic_ostringstream`, `ostringstream`, `wostringstream`.
@@ -137,7 +128,7 @@ class RegexConstructor extends IndirectDynamicMemoryAllocatingFunction {
137128
}
138129

139130
/**
140-
* A constructor of a type-erasing wrapper that may allocate via operator new.
131+
* A constructor of a type-erasing wrapper that may allocate via `operator new`.
141132
* SBO (small buffer optimization) is not guaranteed by the standard.
142133
* Includes `std::function` and `std::any`.
143134
*/
@@ -151,7 +142,7 @@ class TypeErasureConstructor extends IndirectDynamicMemoryAllocatingFunction {
151142
/**
152143
* A constructor of a type that heap-allocates shared state for
153144
* cross-object or cross-thread communication.
154-
* Includes promise, future, shared_future, packaged_task, and locale.
145+
* Includes `promise`, `future`, `shared_future`, `packaged_task`, and `locale`.
155146
*/
156147
class SharedStateConstructor extends IndirectDynamicMemoryAllocatingFunction {
157148
SharedStateConstructor() {
@@ -182,6 +173,16 @@ class FilesystemPathConstructor extends IndirectDynamicMemoryAllocatingFunction
182173
}
183174
}
184175

176+
/**
177+
* A constructor of `std::valarray` that allocates dynamic memory.
178+
*/
179+
class ValarrayConstructor extends IndirectDynamicMemoryAllocatingFunction {
180+
ValarrayConstructor() {
181+
this instanceof Constructor and
182+
this.getDeclaringType().hasQualifiedName("std", "valarray")
183+
}
184+
}
185+
185186
/**
186187
* A smart pointer factory function that allocates dynamic memory.
187188
* Includes `make_unique`, `make_shared`, and `allocate_shared`.
@@ -206,16 +207,17 @@ abstract class DynamicMemoryDeallocatingFunction extends Function { }
206207

207208
/**
208209
* A function that directly deallocates dynamic memory.
209-
* Includes C deallocation functions (`free`)
210-
* and C++ deallocation functions (`operator delete`, `operator delete[]`).
210+
*
211+
* Includes C deallocation functions (`free`) and C++ deallocation functions
212+
* (`operator delete`, `operator delete[]`).
211213
*/
212214
class DirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction {
213215
DirectDynamicMemoryDeallocatingFunction() { this instanceof DeallocationFunction }
214216
}
215217

216218
/**
217-
* A function that indirectly deallocates dynamic memory through
218-
* standard library classes and their member functions (e.g. `std::allocator::deallocate`).
219+
* A function that indirectly deallocates dynamic memory through standard
220+
* library classes and their member functions (e.g. `std::allocator::deallocate`).
219221
*/
220222
class IndirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction {
221223
IndirectDynamicMemoryDeallocatingFunction() {
@@ -228,18 +230,26 @@ from FunctionCall call, string message
228230
where
229231
not isExcluded(call, Banned7Package::dynamicMemoryShouldNotBeUsedQuery()) and
230232
(
231-
// Direct allocation: malloc, calloc, realloc, aligned_alloc, operator new, operator new[]
233+
/* 1. Direct allocation: malloc, calloc, realloc, aligned_alloc, operator new, operator new[]. */
232234
call.getTarget() instanceof DirectDynamicMemoryAllocatingFunction and
233235
message = "Call to dynamic memory allocating function '" + call.getTarget().getName() + "'."
234236
or
235-
// Indirect allocation: std library types that allocate internally
237+
/* 2. Indirect allocation: std library types that allocate internally */
236238
call.getTarget() instanceof IndirectDynamicMemoryAllocatingFunction and
237-
message =
238-
"Call to '" + call.getTarget().getName() +
239-
"' that dynamically allocates memory via the standard library."
239+
(
240+
if call.getTarget() instanceof AllocatorContructor
241+
then message = "Call to '" + call.getTarget().getName() + "' that uses 'std::allocator<T>'."
242+
else
243+
message =
244+
"Call to '" + call.getTarget().getName() +
245+
"' that dynamically allocates memory via the standard library."
246+
)
240247
or
241-
// Deallocation: free, operator delete, operator delete[], std::allocator::deallocate
242-
// Excludes realloc (already caught as allocation).
248+
/*
249+
* 3. Deallocation: free, operator delete, operator delete[], std::allocator::deallocate.
250+
* Excludes realloc (already caught as allocation).
251+
*/
252+
243253
call.getTarget() instanceof DynamicMemoryDeallocatingFunction and
244254
not call.getTarget() instanceof DynamicMemoryAllocatingFunction and
245255
message = "Call to dynamic memory deallocating function '" + call.getTarget().getName() + "'."

0 commit comments

Comments
 (0)