Skip to content

Commit e33e379

Browse files
committed
Finalize first draft
1 parent 9075324 commit e33e379

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ import cpp
1515
import codingstandards.cpp.misra
1616
import codingstandards.cpp.SmartPointers
1717

18+
class DynamicMemoryManagementFunction extends Function {
19+
string description;
20+
21+
DynamicMemoryManagementFunction() {
22+
(this instanceof AllocationFunction or this instanceof AlignedAlloc) and
23+
description = "an expression that dynamically allocates memory"
24+
or
25+
this instanceof DeallocationFunction and
26+
description = "an expression that dynamically deallocates memory"
27+
or
28+
this instanceof AllocateOrDeallocateStdlibMemberFunction and
29+
description = "a standard library function that manages memory manually"
30+
or
31+
this instanceof UniquePointerReleaseFunction and
32+
description = "`std::unique_ptr::release`"
33+
}
34+
35+
string describe() { result = description }
36+
}
37+
1838
/**
1939
* A function that has namespace `std` and has name `allocate` or `deallocate`, including but not limited to:
2040
* - `std::allocator<T>::allocate(std::size_t)`
@@ -36,36 +56,24 @@ class AlignedAlloc extends Function {
3656
AlignedAlloc() { this.hasGlobalOrStdName("aligned_alloc") }
3757
}
3858

39-
/**
40-
* An expression that wraps `Alloc::isMemoryManagementExpr/0` and adds calls to `aligned_alloc`
41-
* to the set detected by it.
42-
*/
43-
class MemoryManagementExpr extends Expr {
44-
MemoryManagementExpr() {
45-
isMemoryManagementExpr(this) or this.(FunctionCall).getTarget() instanceof AlignedAlloc
46-
}
59+
class UniquePointerReleaseFunction extends MemberFunction {
60+
UniquePointerReleaseFunction() { this.getClassAndName("release") instanceof AutosarUniquePointer }
4761
}
4862

4963
from Expr expr, string message
5064
where
5165
not isExcluded(expr, Memory5Package::dynamicMemoryManagedManuallyQuery()) and
52-
(
53-
/* ===== 1. The expression is a use of non-placement `new`/ `new[]`, or a `delete`. ===== */
54-
/* ===== 2. The expression is a call to `malloc` / `calloc` / `aligned_alloc` or to `free`. ===== */
55-
expr instanceof MemoryManagementExpr and
56-
message = "This expression dynamically allocates or deallocates memory."
57-
or
58-
/* ===== 3. The expression is a call to a member function named `allocate` or `deallocate` in namespace `std`. ===== */
59-
exists(AllocateOrDeallocateStdlibMemberFunction allocateOrDeallocate |
60-
expr.(FunctionCall).getTarget() = allocateOrDeallocate and
61-
message =
62-
"This expression uses a standard library function `" +
63-
allocateOrDeallocate.getQualifiedName() + "` that manages memory manually."
64-
)
66+
exists(DynamicMemoryManagementFunction dynamicMemoryManagementFunction |
67+
/* ===== 1. The expression calls one of the dynamic memory management functions. ===== */
68+
expr = dynamicMemoryManagementFunction.getACallToThisFunction() and
69+
message =
70+
"This expression is a call to `" + dynamicMemoryManagementFunction.getName() + "` which is " +
71+
dynamicMemoryManagementFunction.describe() + "."
6572
or
66-
/* ===== 4. The expression is a call to `std::unique_ptr::release`. ==== */
67-
exists(AutosarUniquePointer uniquePtr | expr = uniquePtr.getAReleaseCall()) and
73+
/* ===== 2. The expression takes address of the dynamic memory management functions. ===== */
74+
expr = dynamicMemoryManagementFunction.getAnAccess() and
6875
message =
69-
"This expression is a call to `std::unique_ptr::release` that manages memory manually."
76+
"This expression takes address of `" + dynamicMemoryManagementFunction.getName() +
77+
"` which is " + dynamicMemoryManagementFunction.describe() + "."
7078
)
7179
select expr, message

0 commit comments

Comments
 (0)