Skip to content

Commit 9f112e7

Browse files
Merge pull request #1105 from github/jeongsoolee09/MISRA-C++-2023-Banned7
Add Banned7 package
2 parents b03547b + e856728 commit 9f112e7

50 files changed

Lines changed: 1446 additions & 108 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** Provides classes for modeling dynamic memory allocation and deallocation functions. */
2+
3+
import cpp
4+
5+
/**
6+
* A function that has namespace `std` and has name `allocate` or `deallocate`, including but
7+
* not limited to:
8+
* - `std::allocator<T>::allocate(std::size_t)`
9+
* - `std::allocator<T>::deallocate(T*, std::size_t)`
10+
* - `std::pmr::memory_resource::allocate(std::size_t, std::size_t)`
11+
* - `std::pmr::memory_resource::deallocate(void*, std::size_t, std::size_t)`
12+
*/
13+
class AllocateOrDeallocateStdlibMemberFunction extends MemberFunction {
14+
AllocateOrDeallocateStdlibMemberFunction() {
15+
this.getName() in ["allocate", "deallocate"] and
16+
this.getNamespace().getParentNamespace*() instanceof StdNamespace
17+
}
18+
}
19+
20+
class StdAllocator extends Class {
21+
StdAllocator() { this.hasGlobalOrStdName("allocator") }
22+
}
23+
24+
class StdPmrMemoryResource extends Class {
25+
StdPmrMemoryResource() { this.hasQualifiedName("std::pmr", "memory_resource") }
26+
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Provides classes to help reasoning about `operator new`, `operator new[]`,
3+
* `operator delete`, and `operator delete[]`.
4+
*
5+
* These are described in section [support.dynamic] of the C++ standard.
6+
*/
7+
18
import cpp
29
import codingstandards.cpp.Handlers
310

@@ -21,6 +28,13 @@ abstract class OperatorNewOrDelete extends Operator {
2128
}
2229
}
2330

31+
/**
32+
* An `operator new` and `operator new[]` function described in [new.delete.single]
33+
* and [new.delete.array], respectively.
34+
*
35+
* Note that these do not include [new.delete.placement]. These are captured in
36+
* `PlacementOperatorNew`.
37+
*/
2438
class ReplaceableOperatorNew extends OperatorNewOrDelete {
2539
ReplaceableOperatorNew() {
2640
this.getName().regexpMatch("operator new(\\[\\])?") and
@@ -34,6 +48,12 @@ class ReplaceableOperatorNew extends OperatorNewOrDelete {
3448
}
3549
}
3650

51+
/**
52+
* `operator new`, `operator new[]`, `operator delete`, or `operator delete[]` functions
53+
* that are very likely provided by the user.
54+
*
55+
* Note that this captures _any_ function that has one of the above four names.
56+
*/
3757
class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
3858
CustomOperatorNewOrDelete() {
3959
this.hasDefinition() and
@@ -60,8 +80,18 @@ class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
6080
}
6181
}
6282

83+
/**
84+
* The replaceable `operator new` or `operator new[]` functions that have custom
85+
* definitions provided by the user.
86+
*
87+
* Also see `CustomReplaceableOperatorDelete`.
88+
*/
6389
class CustomReplaceableOperatorNew extends CustomOperatorNewOrDelete, ReplaceableOperatorNew { }
6490

91+
/**
92+
* An `operator delete` or `operator delete[]` deallocation function described in
93+
* [new.delete.single] and [new.delete.array], respectively.
94+
*/
6595
class ReplaceableOperatorDelete extends OperatorNewOrDelete {
6696
ReplaceableOperatorDelete() {
6797
this.getName().regexpMatch("operator delete(\\[\\])?") and
@@ -85,6 +115,12 @@ class ReplaceableOperatorDelete extends OperatorNewOrDelete {
85115
}
86116
}
87117

118+
/**
119+
* The replaceable `operator new` or `operator new[]` functions that have custom
120+
* definitions provided by the user.
121+
*
122+
* Also see `CustomReplaceableOperatorNew`.
123+
*/
88124
class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, ReplaceableOperatorDelete {
89125
CustomReplaceableOperatorDelete getPartner() {
90126
if this.getAParameter().getType() instanceof Size_t
@@ -95,3 +131,18 @@ class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, Replace
95131
else result.getPartner() = this
96132
}
97133
}
134+
135+
/**
136+
* An `operator new` or `operator new[]` allocation function called by a placement-new expression,
137+
* as described in [new.delete.placement].
138+
*
139+
* The operator functions have a `std::size_t` as their first parameter and a
140+
* `void*` parameter somewhere in the rest of the parameter list.
141+
*/
142+
class PlacementOperatorNew extends AllocationFunction {
143+
PlacementOperatorNew() {
144+
this.getName() in ["operator new", "operator new[]"] and
145+
this.getParameter(0).getType().resolveTypedefs*() instanceof Size_t and
146+
this.getAParameter().getUnderlyingType() instanceof VoidPointerType
147+
}
148+
}
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 Banned7Query = TDynamicMemoryShouldNotBeUsedQuery()
7+
8+
predicate isBanned7QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `dynamicMemoryShouldNotBeUsed` query
11+
Banned7Package::dynamicMemoryShouldNotBeUsedQuery() and
12+
queryId =
13+
// `@id` for the `dynamicMemoryShouldNotBeUsed` query
14+
"cpp/misra/dynamic-memory-should-not-be-used" and
15+
ruleId = "RULE-21-6-1" and
16+
category = "advisory"
17+
}
18+
19+
module Banned7Package {
20+
Query dynamicMemoryShouldNotBeUsedQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `dynamicMemoryShouldNotBeUsed` query
24+
TQueryCPP(TBanned7PackageQuery(TDynamicMemoryShouldNotBeUsedQuery()))
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Banned3
99
import Banned4
1010
import Banned5
1111
import Banned6
12+
import Banned7
1213
import Banned8
1314
import BannedAPIs
1415
import BannedFunctions
@@ -116,6 +117,7 @@ newtype TCPPQuery =
116117
TBanned4PackageQuery(Banned4Query q) or
117118
TBanned5PackageQuery(Banned5Query q) or
118119
TBanned6PackageQuery(Banned6Query q) or
120+
TBanned7PackageQuery(Banned7Query q) or
119121
TBanned8PackageQuery(Banned8Query q) or
120122
TBannedAPIsPackageQuery(BannedAPIsQuery q) or
121123
TBannedFunctionsPackageQuery(BannedFunctionsQuery q) or
@@ -223,6 +225,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
223225
isBanned4QueryMetadata(query, queryId, ruleId, category) or
224226
isBanned5QueryMetadata(query, queryId, ruleId, category) or
225227
isBanned6QueryMetadata(query, queryId, ruleId, category) or
228+
isBanned7QueryMetadata(query, queryId, ruleId, category) or
226229
isBanned8QueryMetadata(query, queryId, ruleId, category) or
227230
isBannedAPIsQueryMetadata(query, queryId, ruleId, category) or
228231
isBannedFunctionsQueryMetadata(query, queryId, ruleId, category) or
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <any.h>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _GHLIBCPP_ANY
2+
#define _GHLIBCPP_ANY
3+
namespace std {
4+
5+
class any {
6+
public:
7+
any();
8+
any(const any &);
9+
any(any &&);
10+
~any();
11+
template <typename T> any(T &&);
12+
};
13+
14+
} // namespace std
15+
16+
#endif // _GHLIBCPP_ANY
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <bitset.h>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// stubs/bitset
2+
3+
#ifndef _GHLIBCPP_BITSET
4+
#define _GHLIBCPP_BITSET
5+
6+
#include <cstddef>
7+
8+
namespace std {
9+
10+
template <std::size_t N> class bitset {
11+
public:
12+
bitset();
13+
bitset(unsigned long long);
14+
};
15+
16+
} // namespace std
17+
18+
#endif // _GHLIBCPP_BITSET

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#ifndef _GHLIBCPP_DEQUE
22
#define _GHLIBCPP_DEQUE
3-
#include <iterator>
4-
#include <string>
53
#include "memory.h"
6-
#include <initializer_list>
74
#include <empty.h>
5+
#include <initializer_list>
6+
#include <iterator>
7+
#include <string>
88

99
namespace std {
1010
template <class T, class Allocator = std::allocator<T>> class deque {
@@ -14,8 +14,11 @@ template <class T, class Allocator = std::allocator<T>> class deque {
1414
typedef value_type &reference;
1515
typedef const value_type &const_reference;
1616

17-
deque() = default;
17+
deque();
18+
deque(const deque &);
19+
deque(deque &&);
1820
deque(std::initializer_list<T>, const Allocator & = Allocator());
21+
~deque();
1922

2023
typedef __iterator<T> iterator;
2124
typedef __iterator<T> const_iterator;
@@ -40,4 +43,4 @@ template <class T, class Allocator = std::allocator<T>> class deque {
4043
};
4144
} // namespace std
4245

43-
#endif // _GHLIBCPP_DEQUE
46+
#endif // _GHLIBCPP_DEQUE
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <filesystem.h>

0 commit comments

Comments
 (0)