C++: Give OperatorNewAllocationFunction, OperatorDeleteAllocationFunction proper interfaces.

This commit is contained in:
Geoffrey White
2020-11-04 08:56:58 +00:00
parent 7f54379a0c
commit b5326b3937
6 changed files with 28 additions and 18 deletions

View File

@@ -5,8 +5,6 @@
import cpp import cpp
import semmle.code.cpp.controlflow.SSA import semmle.code.cpp.controlflow.SSA
import semmle.code.cpp.dataflow.DataFlow import semmle.code.cpp.dataflow.DataFlow
import semmle.code.cpp.models.implementations.Allocation
import semmle.code.cpp.models.implementations.Deallocation
/** /**
* Holds if `alloc` is a use of `malloc` or `new`. `kind` is * Holds if `alloc` is a use of `malloc` or `new`. `kind` is

View File

@@ -6,7 +6,6 @@ import semmle.code.cpp.Element
private import semmle.code.cpp.Enclosing private import semmle.code.cpp.Enclosing
private import semmle.code.cpp.internal.ResolveClass private import semmle.code.cpp.internal.ResolveClass
private import semmle.code.cpp.internal.AddressConstantExpression private import semmle.code.cpp.internal.AddressConstantExpression
private import semmle.code.cpp.models.implementations.Allocation
/** /**
* A C/C++ expression. * A C/C++ expression.

View File

@@ -237,12 +237,10 @@ private class SizelessAllocationFunction extends AllocationFunction {
} }
/** /**
* An `operator new` or `operator new[]` function that may be associated with `new` or * Implements `OperatorNewAllocationFunction`.
* `new[]` expressions. Note that `new` and `new[]` are not function calls, but these
* functions may also be called directly.
*/ */
class OperatorNewAllocationFunction extends AllocationFunction { private class OperatorNewAllocationFunctionImpl extends OperatorNewAllocationFunction {
OperatorNewAllocationFunction() { OperatorNewAllocationFunctionImpl() {
exists(string name | exists(string name |
hasGlobalName(name) and hasGlobalName(name) and
( (
@@ -259,11 +257,7 @@ class OperatorNewAllocationFunction extends AllocationFunction {
override predicate requiresDealloc() { not exists(getPlacementArgument()) } override predicate requiresDealloc() { not exists(getPlacementArgument()) }
/** override int getPlacementArgument() {
* Gets the position of the placement pointer if this is a placement
* `operator new` function.
*/
int getPlacementArgument() {
getNumberOfParameters() = 2 and getNumberOfParameters() = 2 and
getParameter(1).getType() instanceof VoidPointerType and getParameter(1).getType() instanceof VoidPointerType and
result = 1 result = 1

View File

@@ -90,12 +90,10 @@ private class StandardDeallocationFunction extends DeallocationFunction {
} }
/** /**
* An `operator delete` or `operator delete[]` function that may be associated * Implements `OperatorDeleteDeallocationFunction`.
* with `delete` or `delete[]` expressions. Note that `delete` and `delete[]`
* are not function calls, but these functions may also be called directly.
*/ */
class OperatorDeleteDeallocationFunction extends DeallocationFunction { private class OperatorDeleteDeallocationFunctionImpl extends OperatorDeleteDeallocationFunction {
OperatorDeleteDeallocationFunction() { OperatorDeleteDeallocationFunctionImpl() {
exists(string name | exists(string name |
hasGlobalName(name) and hasGlobalName(name) and
( (

View File

@@ -85,3 +85,16 @@ abstract class AllocationExpr extends Expr {
*/ */
predicate requiresDealloc() { any() } predicate requiresDealloc() { any() }
} }
/**
* An `operator new` or `operator new[]` function that may be associated with
* `new` or `new[]` expressions. Note that `new` and `new[]` are not function
* calls, but these functions may also be called directly.
*/
abstract class OperatorNewAllocationFunction extends AllocationFunction {
/**
* Gets the position of the placement pointer if this is a placement
* `operator new` function.
*/
int getPlacementArgument() { none()}
}

View File

@@ -30,3 +30,11 @@ abstract class DeallocationExpr extends Expr {
*/ */
Expr getFreedExpr() { none() } Expr getFreedExpr() { none() }
} }
/**
* An `operator delete` or `operator delete[]` function that may be associated
* with `delete` or `delete[]` expressions. Note that `delete` and `delete[]`
* are not function calls, but these functions may also be called directly.
*/
abstract class OperatorDeleteDeallocationFunction extends DeallocationFunction {
}