Skip to content

Commit 3e795fa

Browse files
committed
Checkpoint
1 parent c7e2e87 commit 3e795fa

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

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

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,49 @@
1414
import cpp
1515
import codingstandards.cpp.misra
1616

17-
from
17+
/**
18+
* A function that has namespace `std` and has name `allocate` or `deallocate`, including but not limited to:
19+
* - `std::allocator<T>::allocate(std::size_t)`
20+
* - `std::allocator<T>::dellocate(T*, std::size_t)`
21+
* - `std::pmr::memory_resource::allocate(std::size_t, std::size_t)`
22+
* - `std::pmr::memory_resource::allocate(std::size_t, std::size_t)`
23+
*/
24+
class AllocateOrDeallocateStdlibMemberFunction extends MemberFunction {
25+
AllocateOrDeallocateStdlibMemberFunction() {
26+
this.getName() in ["allocate", "deallocate"] and
27+
this.getNamespace().getParentNamespace*() instanceof StdNamespace
28+
}
29+
}
30+
31+
/**
32+
* The `std::aligned_alloc` (`<cstdlib>`) or `::aligned_alloc` (`<stdlib.h>`) function.
33+
*/
34+
class AlignedAlloc extends Function {
35+
AlignedAlloc() { this.hasGlobalOrStdName("aligned_alloc") }
36+
}
37+
38+
/**
39+
* An expression that wraps `Alloc::isMemoryManagementExpr/0` and adds calls to `aligned_alloc`
40+
* to the set detected by it.
41+
*/
42+
class MemoryManagementExpr extends Expr {
43+
MemoryManagementExpr() {
44+
isMemoryManagementExpr(this) or this.(FunctionCall).getTarget() instanceof AlignedAlloc
45+
}
46+
}
47+
48+
from Expr expr
1849
where
19-
not isExcluded(x, Memory5Package::dynamicMemoryManagedManuallyQuery()) and
20-
select
50+
not isExcluded(expr, Memory5Package::dynamicMemoryManagedManuallyQuery()) and
51+
(
52+
/* ===== 1. The expression is a use of non-placement `new`/ `new[]`, or a `delete`. ===== */
53+
/* ===== 2. The expression is a call to malloc / calloc / or to `free`. ===== */
54+
expr instanceof MemoryManagementExpr
55+
or
56+
/* ===== 3. The expression is a call to a member function named `allocate` or `deallocate` in namespace `std`. ===== */
57+
expr.(FunctionCall).getTarget() instanceof AllocateOrDeallocateStdlibMemberFunction
58+
or
59+
/* ===== 4. The expression is a ==== */
60+
none()
61+
)
62+
select expr, "TODO"

0 commit comments

Comments
 (0)