@@ -9,6 +9,7 @@ import codingstandards.cpp.Customizations
9
9
import codingstandards.cpp.Exclusions
10
10
import semmle.code.cpp.dataflow.new.DataFlow
11
11
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
12
+ import semmle.code.cpp.rangeanalysis.RangeAnalysisUtils
12
13
import codeql.util.Boolean
13
14
14
15
abstract class DoNotUsePointerArithmeticToAddressDifferentArraysSharedQuery extends Query { }
@@ -68,11 +69,13 @@ int elementSize(Type type, Boolean deref) {
68
69
* length depends on `elementSize()` of the original pointed-to type.
69
70
*/
70
71
class CastedToBytePointer extends ArrayLikeAccess , Conversion {
72
+ /** The sizeof() the pointed-to type */
71
73
int size ;
72
74
73
75
CastedToBytePointer ( ) {
74
76
getType ( ) .( PointerType ) .getBaseType ( ) .getSize ( ) = 1 and
75
- size = elementSize ( getExpr ( ) .getType ( ) , true )
77
+ size = elementSize ( getExpr ( ) .getType ( ) , true ) and
78
+ size > 1
76
79
}
77
80
78
81
override Element getElement ( ) { result = this }
@@ -138,7 +141,7 @@ module ArrayToArrayExprFlow = DataFlow::Global<ArrayToArrayExprConfig>;
138
141
139
142
/** Holds if the address taken expression `addressOf` takes the address of an array element at `index` of `array`. */
140
143
predicate pointerOperandCreation ( AddressOfExpr addressOf , ArrayLikeAccess array , int index ) {
141
- exists ( ArrayExpr ae |
144
+ exists ( ArrayExpr ae , Expr arrayOffset |
142
145
(
143
146
ArrayToArrayExprFlow:: flow ( array .getNode ( ) , DataFlow:: exprNode ( ae .getArrayBase ( ) ) ) and
144
147
array instanceof ArrayVariableAccess
@@ -149,7 +152,10 @@ predicate pointerOperandCreation(AddressOfExpr addressOf, ArrayLikeAccess array,
149
152
// flow() may hold for `ArrayVariableAccess` in the above, even though they aren't sources
150
153
array instanceof CastedToBytePointer
151
154
) and
152
- index = lowerBound ( ae .getArrayOffset ( ) .getFullyConverted ( ) ) and
155
+ arrayOffset = ae .getArrayOffset ( ) .getFullyConverted ( ) and
156
+ index = lowerBound ( arrayOffset ) and
157
+ // This case typically indicates range analysis has gone wrong:
158
+ not index = exprMaxVal ( arrayOffset ) and
153
159
addressOf .getOperand ( ) = ae
154
160
)
155
161
}
0 commit comments