@@ -3,6 +3,29 @@ private import semmle.code.cpp.ir.IR
3
3
private import codeql.typeflow.TypeFlow
4
4
5
5
private module Input implements TypeFlowInput< Location > {
6
+ /** Holds if `alloc` dynamically allocates a single object. */
7
+ private predicate isSingleObjectAllocation ( AllocationExpr alloc ) {
8
+ // i.e., `new int`;
9
+ alloc instanceof NewExpr
10
+ or
11
+ // i.e., `malloc(sizeof(int))`
12
+ exists ( SizeofTypeOperator sizeOf | sizeOf = alloc .getSizeExpr ( ) |
13
+ not sizeOf .getTypeOperand ( ) .getUnspecifiedType ( ) instanceof ArrayType
14
+ )
15
+ }
16
+
17
+ /**
18
+ * Holds if `i` is the result of a dynamic allocation.
19
+ *
20
+ * `isObject` is `true` if the allocation allocated a single object,
21
+ * and `false` otherwise.
22
+ */
23
+ private predicate isAllocation ( Instruction i , boolean isObject ) {
24
+ exists ( AllocationExpr alloc | alloc = i .getUnconvertedResultExpression ( ) |
25
+ if isSingleObjectAllocation ( alloc ) then isObject = true else isObject = false
26
+ )
27
+ }
28
+
6
29
private predicate hasExactSingleType ( Instruction i ) {
7
30
// The address of a variable is always a single object
8
31
i instanceof VariableAddressInstruction
@@ -14,23 +37,16 @@ private module Input implements TypeFlowInput<Location> {
14
37
i instanceof InitializeThisInstruction
15
38
or
16
39
// An allocation of a non-array object
17
- exists ( AllocationExpr alloc | alloc = i .getUnconvertedResultExpression ( ) |
18
- // i.e., `new int`;
19
- alloc instanceof NewExpr
20
- or
21
- // i.e., `malloc(sizeof(int))`
22
- exists ( SizeofTypeOperator sizeOf | sizeOf = alloc .getSizeExpr ( ) |
23
- not sizeOf .getTypeOperand ( ) .getUnspecifiedType ( ) instanceof ArrayType
24
- )
25
- )
40
+ isAllocation ( i , true )
26
41
}
27
42
28
43
private predicate hasExactBufferType ( Instruction i ) {
29
44
// Anything with an array type is a buffer
30
45
i .getResultLanguageType ( ) .hasUnspecifiedType ( any ( ArrayType at ) , false )
31
46
or
32
- not hasExactSingleType ( i ) and
33
- i .getUnconvertedResultExpression ( ) instanceof AllocationExpr
47
+ // An allocation expression that we couldn't conclude allocated a single
48
+ // expression is assigned a buffer type.
49
+ isAllocation ( i , false )
34
50
}
35
51
36
52
private newtype TTypeFlowNode =
0 commit comments