1515
1616import cpp
1717import codingstandards.cpp.misra
18-
19- class PlacementNewOrNewArrayAllocationFunction extends AllocationFunction {
20- /* NOTE: Duplicate with RULE-21-6-2 */
21- PlacementNewOrNewArrayAllocationFunction ( ) {
22- this .getName ( ) in [ "operator new" , "operator new[]" ] and
23- this .getParameter ( 0 ) .getType ( ) .resolveTypedefs * ( ) instanceof Size_t and
24- this .getAParameter ( ) .getUnderlyingType ( ) instanceof VoidPointerType
25- }
26- }
27-
28- /**
29- * A function that has namespace `std` and has name `allocate` or `deallocate`, including but not limited to:
30- * - `std::allocator<T>::allocate(std::size_t)`
31- * - `std::allocator<T>::dellocate(T*, std::size_t)`
32- * - `std::pmr::memory_resource::allocate(std::size_t, std::size_t)`
33- * - `std::pmr::memory_resource::deallocate(void*, std::size_t, std::size_t)`
34- */
35- class AllocateOrDeallocateStdlibMemberFunction extends MemberFunction {
36- /* NOTE: Duplicate with RULE-21-6-2 */
37- AllocateOrDeallocateStdlibMemberFunction ( ) {
38- this .getName ( ) in [ "allocate" , "deallocate" ] and
39- this .getNamespace ( ) .getParentNamespace * ( ) instanceof StdNamespace
40- }
41- }
18+ import codingstandards.cpp.DynamicMemory
4219
4320/**
4421 * A function that directly or indirectly allocates dynamic memory.
@@ -56,6 +33,8 @@ class DirectDynamicMemoryAllocatingFunction extends DynamicMemoryAllocatingFunct
5633 DirectDynamicMemoryAllocatingFunction ( ) {
5734 this instanceof AllocationFunction and
5835 not this instanceof PlacementNewOrNewArrayAllocationFunction
36+ or
37+ this .hasGlobalOrStdName ( "aligned_alloc" )
5938 }
6039}
6140
@@ -226,19 +205,48 @@ abstract class DynamicMemoryDeallocatingFunction extends Function { }
226205
227206/**
228207 * A function that directly deallocates dynamic memory.
229- * Includes C allocation functions (`free`)
230- * and C++ allocation functions (`operator delete`, `operator delete[]`).
208+ * Includes C deallocation functions (`free`)
209+ * and C++ deallocation functions (`operator delete`, `operator delete[]`).
231210 */
232- class DirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction { }
211+ class DirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction {
212+ DirectDynamicMemoryDeallocatingFunction ( ) {
213+ this instanceof DeallocationFunction
214+ }
215+ }
233216
234217/**
235- * A function that indirectly allocates dynamic memory through
218+ * A function that indirectly deallocates dynamic memory through
236219 * standard library classes and their member functions (e.g. `std::allocator::deallocate`).
237220 */
238- class IndirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction { }
221+ class IndirectDynamicMemoryDeallocatingFunction extends DynamicMemoryDeallocatingFunction {
222+ IndirectDynamicMemoryDeallocatingFunction ( ) {
223+ this instanceof AllocateOrDeallocateStdlibMemberFunction and
224+ this .getName ( ) = "deallocate"
225+ }
226+ }
239227
240- from FunctionCall call
228+ from FunctionCall call , string message
241229where
242230 not isExcluded ( call , Banned7Package:: dynamicMemoryShouldNotBeUsedQuery ( ) ) and
243- call .getTarget ( ) instanceof DynamicMemoryAllocatingFunction
244- select call , call .getTarget ( ) .toString ( )
231+ (
232+ // Direct allocation: malloc, calloc, realloc, aligned_alloc, operator new, operator new[]
233+ call .getTarget ( ) instanceof DirectDynamicMemoryAllocatingFunction and
234+ message =
235+ "Call to dynamic memory allocating function '" + call .getTarget ( ) .getName ( ) +
236+ "'."
237+ or
238+ // Indirect allocation: std library types that allocate internally
239+ call .getTarget ( ) instanceof IndirectDynamicMemoryAllocatingFunction and
240+ message =
241+ "Call to '" + call .getTarget ( ) .getName ( ) +
242+ "' that dynamically allocates memory via the standard library."
243+ or
244+ // Deallocation: free, operator delete, operator delete[], std::allocator::deallocate
245+ // Excludes realloc (already caught as allocation).
246+ call .getTarget ( ) instanceof DynamicMemoryDeallocatingFunction and
247+ not call .getTarget ( ) instanceof DynamicMemoryAllocatingFunction and
248+ message =
249+ "Call to dynamic memory deallocating function '" + call .getTarget ( ) .getName ( ) +
250+ "'."
251+ )
252+ select call , message
0 commit comments