@@ -3,6 +3,29 @@ private import semmle.code.cpp.ir.IR
33private import codeql.typeflow.TypeFlow
44
55private 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+
629 private predicate hasExactSingleType ( Instruction i ) {
730 // The address of a variable is always a single object
831 i instanceof VariableAddressInstruction
@@ -14,23 +37,16 @@ private module Input implements TypeFlowInput<Location> {
1437 i instanceof InitializeThisInstruction
1538 or
1639 // 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 )
2641 }
2742
2843 private predicate hasExactBufferType ( Instruction i ) {
2944 // Anything with an array type is a buffer
3045 i .getResultLanguageType ( ) .hasUnspecifiedType ( any ( ArrayType at ) , false )
3146 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 )
3450 }
3551
3652 private newtype TTypeFlowNode =
0 commit comments