Skip to content

Commit 68892e6

Browse files
Merge pull request #1048 from github/jeongsoolee09/MISRA-C++-2023-Memory5-Memory6
Add packages `Memory5` and `Memory6`
2 parents b6be03f + 0c1521c commit 68892e6

File tree

31 files changed

+1714
-67
lines changed

31 files changed

+1714
-67
lines changed

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

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,69 +14,80 @@ class ConstNoThrowTReferenceType extends ReferenceType {
1414
}
1515

1616
/** An `operator` that implements one of the `[replacement.functions]`. */
17-
abstract class CustomOperatorNewOrDelete extends Operator {
17+
abstract class OperatorNewOrDelete extends Operator {
18+
OperatorNewOrDelete() {
19+
this.getName().regexpMatch("operator new(\\[\\])?") or
20+
this.getName().regexpMatch("operator delete(\\[\\])?")
21+
}
22+
}
23+
24+
class ReplaceableOperatorNew extends OperatorNewOrDelete {
25+
ReplaceableOperatorNew() {
26+
this.getName().regexpMatch("operator new(\\[\\])?") and
27+
this.getParameter(0).getType() instanceof Size_t and
28+
(
29+
this.getNumberOfParameters() = 1
30+
or
31+
this.getNumberOfParameters() = 2 and
32+
this.getParameter(1).getType() instanceof ConstNoThrowTReferenceType
33+
)
34+
}
35+
}
36+
37+
class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
1838
CustomOperatorNewOrDelete() {
39+
this.hasDefinition() and
1940
// Not in the standard library
20-
exists(getFile().getRelativePath()) and
41+
exists(this.getFile().getRelativePath()) and
2142
// Not in a file called `new`, which is likely to be a copy of the standard library
2243
// as it is in our tests
23-
not getFile().getBaseName() = "new"
44+
not this.getFile().getBaseName() = "new"
2445
}
2546

2647
/**
2748
* Holds if this is a an allocation function that takes a `const std::nothrow_t&`.
2849
*/
2950
predicate isNoThrowAllocation() {
30-
getAParameter().getType() instanceof ConstNoThrowTReferenceType
51+
this.getAParameter().getType() instanceof ConstNoThrowTReferenceType
3152
}
3253

3354
/** Get the description of this custom allocator. */
3455
string getAllocDescription() {
3556
result =
36-
getName() + "(" +
37-
concat(Parameter p, int i | p = getParameter(i) | p.getType().getName(), "," order by i) +
38-
")"
57+
this.getName() + "(" +
58+
concat(Parameter p, int i | p = this.getParameter(i) | p.getType().getName(), "," order by i)
59+
+ ")"
3960
}
4061
}
4162

42-
class CustomOperatorNew extends CustomOperatorNewOrDelete {
43-
CustomOperatorNew() {
44-
hasDefinition() and
45-
getName().regexpMatch("operator new(\\[\\])?") and
46-
getParameter(0).getType() instanceof Size_t and
47-
(
48-
getNumberOfParameters() = 1
49-
or
50-
getNumberOfParameters() = 2 and
51-
getParameter(1).getType() instanceof ConstNoThrowTReferenceType
52-
)
53-
}
54-
}
63+
class CustomReplaceableOperatorNew extends CustomOperatorNewOrDelete, ReplaceableOperatorNew { }
5564

56-
class CustomOperatorDelete extends CustomOperatorNewOrDelete {
57-
CustomOperatorDelete() {
58-
getName().regexpMatch("operator delete(\\[\\])?") and
59-
getParameter(0).getType() instanceof VoidPointerType and
65+
class ReplaceableOperatorDelete extends OperatorNewOrDelete {
66+
ReplaceableOperatorDelete() {
67+
this.getName().regexpMatch("operator delete(\\[\\])?") and
68+
this.getParameter(0).getType() instanceof VoidPointerType and
6069
(
61-
getNumberOfParameters() = 1
70+
this.getNumberOfParameters() = 1
6271
or
63-
getNumberOfParameters() = 2 and
72+
this.getNumberOfParameters() = 2 and
6473
(
65-
getParameter(1).getType() instanceof ConstNoThrowTReferenceType
74+
this.getParameter(1).getType() instanceof ConstNoThrowTReferenceType
6675
or
67-
getParameter(1).getType() instanceof Size_t
76+
this.getParameter(1).getType() instanceof Size_t
6877
)
6978
or
70-
getNumberOfParameters() = 3 and
79+
this.getNumberOfParameters() = 3 and
7180
(
72-
getParameter(1).getType() instanceof Size_t and
73-
getParameter(2).getType() instanceof ConstNoThrowTReferenceType
81+
this.getParameter(1).getType() instanceof Size_t and
82+
this.getParameter(2).getType() instanceof ConstNoThrowTReferenceType
7483
)
7584
)
7685
}
86+
}
7787

78-
CustomOperatorDelete getPartner() {
79-
if getAParameter().getType() instanceof Size_t
88+
class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, ReplaceableOperatorDelete {
89+
CustomReplaceableOperatorDelete getPartner() {
90+
if this.getAParameter().getType() instanceof Size_t
8091
then
8192
result.getAllocDescription() = this.getAllocDescription().replaceAll(",size_t", "") and
8293
// Linked together in the same target
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Memory5Query = TDynamicMemoryManagedManuallyQuery()
7+
8+
predicate isMemory5QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `dynamicMemoryManagedManually` query
11+
Memory5Package::dynamicMemoryManagedManuallyQuery() and
12+
queryId =
13+
// `@id` for the `dynamicMemoryManagedManually` query
14+
"cpp/misra/dynamic-memory-managed-manually" and
15+
ruleId = "RULE-21-6-2" and
16+
category = "required"
17+
}
18+
19+
module Memory5Package {
20+
Query dynamicMemoryManagedManuallyQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `dynamicMemoryManagedManually` query
24+
TQueryCPP(TMemory5PackageQuery(TDynamicMemoryManagedManuallyQuery()))
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Memory6Query = TAdvancedMemoryManagementUsedQuery()
7+
8+
predicate isMemory6QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `advancedMemoryManagementUsed` query
11+
Memory6Package::advancedMemoryManagementUsedQuery() and
12+
queryId =
13+
// `@id` for the `advancedMemoryManagementUsed` query
14+
"cpp/misra/advanced-memory-management-used" and
15+
ruleId = "RULE-21-6-3" and
16+
category = "required"
17+
}
18+
19+
module Memory6Package {
20+
Query advancedMemoryManagementUsedQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `advancedMemoryManagementUsed` query
24+
TQueryCPP(TMemory6PackageQuery(TAdvancedMemoryManagementUsedQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import Macros
5353
import Memory2
5454
import Memory3
5555
import Memory4
56+
import Memory5
57+
import Memory6
5658
import MoveForward
5759
import Naming
5860
import Naming2
@@ -135,6 +137,8 @@ newtype TCPPQuery =
135137
TMemory2PackageQuery(Memory2Query q) or
136138
TMemory3PackageQuery(Memory3Query q) or
137139
TMemory4PackageQuery(Memory4Query q) or
140+
TMemory5PackageQuery(Memory5Query q) or
141+
TMemory6PackageQuery(Memory6Query q) or
138142
TMoveForwardPackageQuery(MoveForwardQuery q) or
139143
TNamingPackageQuery(NamingQuery q) or
140144
TNaming2PackageQuery(Naming2Query q) or
@@ -217,6 +221,8 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
217221
isMemory2QueryMetadata(query, queryId, ruleId, category) or
218222
isMemory3QueryMetadata(query, queryId, ruleId, category) or
219223
isMemory4QueryMetadata(query, queryId, ruleId, category) or
224+
isMemory5QueryMetadata(query, queryId, ruleId, category) or
225+
isMemory6QueryMetadata(query, queryId, ruleId, category) or
220226
isMoveForwardQueryMetadata(query, queryId, ruleId, category) or
221227
isNamingQueryMetadata(query, queryId, ruleId, category) or
222228
isNaming2QueryMetadata(query, queryId, ruleId, category) or

cpp/common/src/codingstandards/cpp/rules/operatordeletemissingpartner/OperatorDeleteMissingPartner.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Query getQuery() { result instanceof OperatorDeleteMissingPartnerSharedQuery }
3636
* void operator delete[](void*, std::size_t, const std::nothrow_t&)
3737
*/
3838

39-
query predicate problems(CustomOperatorDelete cd, string message) {
39+
query predicate problems(CustomReplaceableOperatorDelete cd, string message) {
4040
not isExcluded(cd, getQuery()) and
4141
not exists(cd.getPartner()) and
4242
if cd.getAParameter().getType() instanceof Size_t

cpp/common/src/codingstandards/cpp/rules/throwingoperatornewreturnsnull/ThrowingOperatorNewReturnsNull.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module NullConfig implements DataFlow::ConfigSig {
3131
}
3232

3333
predicate isSink(DataFlow::Node sink) {
34-
exists(CustomOperatorNew co, ReturnStmt rs |
34+
exists(CustomReplaceableOperatorNew co, ReturnStmt rs |
3535
co.getNumberOfParameters() = 1 and
3636
rs.getEnclosingFunction() = co and
3737
rs.getExpr() = sink.asExpr()
@@ -47,7 +47,7 @@ query predicate problems(
4747
not isExcluded(e, getQuery()) and
4848
NullFlow::flowPath(source, sink) and
4949
sink.getNode().asExpr() = e.getExpr() and
50-
exists(CustomOperatorNew op |
50+
exists(CustomReplaceableOperatorNew op |
5151
message =
5252
op.getAllocDescription() + " may return null instead of throwing a std::bad_alloc exception."
5353
)

cpp/common/src/codingstandards/cpp/rules/throwingoperatornewthrowsinvalidexception/ThrowingOperatorNewThrowsInvalidException.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class ThrowingOperatorNewThrowsInvalidExceptionSharedQuery extends Quer
1515
Query getQuery() { result instanceof ThrowingOperatorNewThrowsInvalidExceptionSharedQuery }
1616

1717
class ThrowingCustomOperatorNew extends ExceptionThrowingFunction {
18-
CustomOperatorNew op;
18+
CustomReplaceableOperatorNew op;
1919

2020
ThrowingCustomOperatorNew() {
2121
this = op and
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Provides models of functions in <memory> that deals with uninitialized memory.
3+
*/
4+
5+
import cpp
6+
7+
abstract class UninitializedMemoryManagementFunction extends Function {
8+
UninitializedMemoryManagementFunction() {
9+
this.getADeclarationLocation().getFile().getShortName() = "memory"
10+
}
11+
}
12+
13+
class UninitializedCopyFunction extends UninitializedMemoryManagementFunction {
14+
UninitializedCopyFunction() { this.hasQualifiedName("std", "uninitialized_copy") }
15+
}
16+
17+
class UninitializedCopyNFunction extends UninitializedMemoryManagementFunction {
18+
UninitializedCopyNFunction() { this.hasQualifiedName("std", "uninitialized_copy_n") }
19+
}
20+
21+
class UninitializedDefaultConstructFunction extends UninitializedMemoryManagementFunction {
22+
UninitializedDefaultConstructFunction() {
23+
this.hasQualifiedName("std", "uninitialized_default_construct")
24+
}
25+
}
26+
27+
class UninitializedDefaultConstructNFunction extends UninitializedMemoryManagementFunction {
28+
UninitializedDefaultConstructNFunction() {
29+
this.hasQualifiedName("std", "uninitialized_default_construct_n")
30+
}
31+
}
32+
33+
class UninitializedValueConstructFunction extends UninitializedMemoryManagementFunction {
34+
UninitializedValueConstructFunction() {
35+
this.hasQualifiedName("std", "uninitialized_value_construct")
36+
}
37+
}
38+
39+
class UninitializedValueConstructNFunction extends UninitializedMemoryManagementFunction {
40+
UninitializedValueConstructNFunction() {
41+
this.hasQualifiedName("std", "uninitialized_value_construct_n")
42+
}
43+
}
44+
45+
class UninitializedMoveFunction extends UninitializedMemoryManagementFunction {
46+
UninitializedMoveFunction() { this.hasQualifiedName("std", "uninitialized_move") }
47+
}
48+
49+
class UninitializedMoveNFunction extends UninitializedMemoryManagementFunction {
50+
UninitializedMoveNFunction() { this.hasQualifiedName("std", "uninitialized_move_n") }
51+
}
52+
53+
class UninitializedFillFunction extends UninitializedMemoryManagementFunction {
54+
UninitializedFillFunction() { this.hasQualifiedName("std", "uninitialized_fill") }
55+
}
56+
57+
class UninitializedFillNFunction extends UninitializedMemoryManagementFunction {
58+
UninitializedFillNFunction() { this.hasQualifiedName("std", "uninitialized_fill_n") }
59+
}
60+
61+
class DestroyFunction extends UninitializedMemoryManagementFunction {
62+
DestroyFunction() { this.hasQualifiedName("std", "destroy") }
63+
}
64+
65+
class DestroyNFunction extends UninitializedMemoryManagementFunction {
66+
DestroyNFunction() { this.hasQualifiedName("std", "destroy_n") }
67+
}
68+
69+
class DestroyAtFunction extends UninitializedMemoryManagementFunction {
70+
DestroyAtFunction() { this.hasQualifiedName("std", "destroy_at") }
71+
}
72+
73+
class LaunderFunction extends UninitializedMemoryManagementFunction {
74+
LaunderFunction() { this.hasQualifiedName("std", "launder") }
75+
}

cpp/common/test/includes/standard-library/cstdlib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ using ::atoll;
1313
using ::exit;
1414
using ::free;
1515
using ::malloc;
16+
using ::calloc;
17+
using ::realloc;
18+
using ::aligned_alloc;
1619
using ::quick_exit;
1720
using ::rand;
1821
using ::strtod;

cpp/common/test/includes/standard-library/functional.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ template <class> class function;
8989
template <class R, class... Args> class function<R(Args...)> {
9090
public:
9191
function();
92-
template <class F> function(F&& f) {
93-
auto fptr = new F(std::forward<F>(f));
94-
}
92+
template <class F> function(F&& f);
9593
template <class F> function &operator=(F &&);
9694
};
9795
} // namespace std
98-
#endif
96+
#endif

0 commit comments

Comments
 (0)