Skip to content

Commit dfbd0ea

Browse files
committed
Impose new hierarchy among operator new / delete
1 parent 2578910 commit dfbd0ea

File tree

5 files changed

+43
-40
lines changed

5 files changed

+43
-40
lines changed

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,70 +21,73 @@ abstract class OperatorNewOrDelete extends Operator {
2121
}
2222
}
2323

24-
/** An `operator` that implements one of the `[replacement.functions]`. */
25-
abstract class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
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 {
2638
CustomOperatorNewOrDelete() {
39+
this.hasDefinition() and
2740
// Not in the standard library
28-
exists(getFile().getRelativePath()) and
41+
exists(this.getFile().getRelativePath()) and
2942
// Not in a file called `new`, which is likely to be a copy of the standard library
3043
// as it is in our tests
31-
not getFile().getBaseName() = "new"
44+
not this.getFile().getBaseName() = "new"
3245
}
3346

3447
/**
3548
* Holds if this is a an allocation function that takes a `const std::nothrow_t&`.
3649
*/
3750
predicate isNoThrowAllocation() {
38-
getAParameter().getType() instanceof ConstNoThrowTReferenceType
51+
this.getAParameter().getType() instanceof ConstNoThrowTReferenceType
3952
}
4053

4154
/** Get the description of this custom allocator. */
4255
string getAllocDescription() {
4356
result =
44-
getName() + "(" +
45-
concat(Parameter p, int i | p = getParameter(i) | p.getType().getName(), "," order by i) +
46-
")"
57+
this.getName() + "(" +
58+
concat(Parameter p, int i | p = this.getParameter(i) | p.getType().getName(), "," order by i)
59+
+ ")"
4760
}
4861
}
4962

50-
class CustomOperatorNew extends CustomOperatorNewOrDelete {
51-
CustomOperatorNew() {
52-
hasDefinition() and
53-
getName().regexpMatch("operator new(\\[\\])?") and
54-
getParameter(0).getType() instanceof Size_t and
55-
(
56-
getNumberOfParameters() = 1
57-
or
58-
getNumberOfParameters() = 2 and
59-
getParameter(1).getType() instanceof ConstNoThrowTReferenceType
60-
)
61-
}
62-
}
63+
class CustomReplaceableOperatorNew extends CustomOperatorNewOrDelete, ReplaceableOperatorNew { }
6364

64-
class CustomOperatorDelete extends CustomOperatorNewOrDelete {
65-
CustomOperatorDelete() {
66-
getName().regexpMatch("operator delete(\\[\\])?") and
67-
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
6869
(
69-
getNumberOfParameters() = 1
70+
this.getNumberOfParameters() = 1
7071
or
71-
getNumberOfParameters() = 2 and
72+
this.getNumberOfParameters() = 2 and
7273
(
73-
getParameter(1).getType() instanceof ConstNoThrowTReferenceType
74+
this.getParameter(1).getType() instanceof ConstNoThrowTReferenceType
7475
or
75-
getParameter(1).getType() instanceof Size_t
76+
this.getParameter(1).getType() instanceof Size_t
7677
)
7778
or
78-
getNumberOfParameters() = 3 and
79+
this.getNumberOfParameters() = 3 and
7980
(
80-
getParameter(1).getType() instanceof Size_t and
81-
getParameter(2).getType() instanceof ConstNoThrowTReferenceType
81+
this.getParameter(1).getType() instanceof Size_t and
82+
this.getParameter(2).getType() instanceof ConstNoThrowTReferenceType
8283
)
8384
)
8485
}
86+
}
8587

86-
CustomOperatorDelete getPartner() {
87-
if getAParameter().getType() instanceof Size_t
88+
class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, ReplaceableOperatorDelete {
89+
CustomReplaceableOperatorDelete getPartner() {
90+
if this.getAParameter().getType() instanceof Size_t
8891
then
8992
result.getAllocDescription() = this.getAllocDescription().replaceAll(",size_t", "") and
9093
// Linked together in the same target

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

cpp/misra/src/rules/RULE-21-6-3/AdvancedMemoryManagementUsed.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AdvancedMemoryManagementFunction extends Function {
3838
class NonStandardNewOrNewArrayOperator extends OperatorNewOrDelete {
3939
NonStandardNewOrNewArrayOperator() {
4040
this.getName() in ["operator new", "operator new[]"] and
41-
not this instanceof CustomOperatorNew // `CustomOperatorNew` only detects replaceable allocation functions.
41+
not this instanceof CustomReplaceableOperatorNew // `CustomReplaceableOperatorNew` only detects replaceable allocation functions.
4242
}
4343
}
4444

@@ -61,7 +61,7 @@ class UserDeclaredOperatorNewOrDelete extends FunctionDeclarationEntry {
6161
class NonStandardDeleteOrDeleteArrayOperator extends OperatorNewOrDelete {
6262
NonStandardDeleteOrDeleteArrayOperator() {
6363
this.getName() in ["operator delete", "operator delete[]"] and
64-
not this instanceof CustomOperatorDelete // `CustomOperatorDelete` only detects replaceable deallocation functions.
64+
not this instanceof CustomReplaceableOperatorDelete // `CustomReplaceableOperatorDelete` only detects replaceable deallocation functions.
6565
}
6666
}
6767

0 commit comments

Comments
 (0)