@@ -27,18 +27,31 @@ class ArrayDeclaration extends VariableDeclarationEntry {
2727 ArrayDeclaration ( ) { this .getType ( ) .getUnderlyingType ( ) instanceof ArrayType }
2828
2929 /**
30- * Gets the declared length of this array at the given level of indirection.
30+ * Gets the declared length of the dimension of this array whose elements have type
31+ * `elementType`. For example, for `int a[2][3]` the length is 2 if `elementType` is `int[3]`,
32+ * and 3 if `elementType` is `int`.
3133 */
32- int getLength ( int indirection ) { result = this .getArrayType ( indirection ) .getArraySize ( ) }
33-
34- private ArrayType getArrayType ( int indirection ) {
35- indirection = 0 and result = this .getType ( ) .getUnderlyingType ( )
36- or
34+ int getLength ( Type elementType ) {
3735 exists ( ArrayType at |
38- at = getArrayType ( indirection - 1 ) and
39- result = at .getBaseType ( ) .getUnderlyingType ( )
36+ at = this .getAnArrayType ( ) and
37+ at .getBaseType ( ) .getUnspecifiedType ( ) = elementType .getUnspecifiedType ( ) and
38+ result = at .getArraySize ( )
4039 )
4140 }
41+
42+ /**
43+ * Gets the number of dimensions of this array.
44+ */
45+ int getNumberOfDimensions ( ) { result = count ( this .getAnArrayType ( ) ) }
46+
47+ /**
48+ * Gets the type of this array, or the type of a nested array in a multi-dimensional array.
49+ */
50+ private ArrayType getAnArrayType ( ) {
51+ result = this .getType ( ) .getUnderlyingType ( )
52+ or
53+ result = this .getAnArrayType ( ) .getBaseType ( ) .getUnderlyingType ( )
54+ }
4255}
4356
4457/**
@@ -154,25 +167,23 @@ class ArrayAllocation extends TArrayAllocation {
154167 }
155168
156169 /**
157- * Gets the number of the object that the array holds. This number is exact for a stack-allocated
158- * array, and the minimum estimated value for a heap-allocated one.
170+ * Gets the number of objects that the array holds when it is accessed through a pointer to
171+ * `elementType`. This number is exact for a stack-allocated array, and the minimum estimated
172+ * value for a heap-allocated one.
173+ *
174+ * For a stack-allocated multi-dimensional array, this is the length of the dimension whose
175+ * elements have type `elementType`. For example, for `int a[2][3]` the length is 2 when the
176+ * array is accessed through a pointer to `int[3]`, and 3 when it is accessed through a pointer
177+ * to `int`.
159178 */
160- int getLength ( DataFlow:: Node node ) {
161- exists ( DataFlow:: IndirectUninitializedNode inode |
162- inode = node and
163- inode .getLocalVariable ( ) = this .asStackAllocation ( ) .getVariable ( ) and
164- result = this .asStackAllocation ( ) .getLength ( inode .getIndirectionIndex ( ) )
165- )
179+ bindingset [ elementType]
180+ int getLength ( Type elementType ) {
181+ result = this .asStackAllocation ( ) .getLength ( elementType )
166182 or
167- node .asInstruction ( ) .( UninitializedInstruction ) .getLocalVariable ( ) =
168- this .asStackAllocation ( ) .getVariable ( ) and
169- result = this .asStackAllocation ( ) .getLength ( 0 )
170- or
171- node .asConvertedExpr ( ) = this .asDynamicAllocation ( ) and
172183 result = this .asDynamicAllocation ( ) .getMinNumElements ( )
173184 or
174185 result = 1 and
175- node . asExpr ( ) = this .asAddressOfExpr ( )
186+ exists ( this .asAddressOfExpr ( ) )
176187 }
177188
178189 Location getLocation ( ) {
@@ -184,7 +195,22 @@ class ArrayAllocation extends TArrayAllocation {
184195 /**
185196 * Gets the node associated with this allocation.
186197 */
187- DataFlow:: Node getNode ( ) { exists ( this .getLength ( result ) ) }
198+ DataFlow:: Node getNode ( ) {
199+ exists ( ArrayDeclaration decl , LocalVariable v |
200+ decl = this .asStackAllocation ( ) and v = decl .getVariable ( )
201+ |
202+ exists ( DataFlow:: IndirectUninitializedNode inode | inode = result |
203+ inode .getLocalVariable ( ) = v and
204+ inode .getIndirectionIndex ( ) < decl .getNumberOfDimensions ( )
205+ )
206+ or
207+ result .asInstruction ( ) .( UninitializedInstruction ) .getLocalVariable ( ) = v
208+ )
209+ or
210+ result .asConvertedExpr ( ) = this .asDynamicAllocation ( )
211+ or
212+ result .asExpr ( ) = this .asAddressOfExpr ( )
213+ }
188214
189215 Expr asExpr ( ) {
190216 result = this .asStackAllocation ( ) .getVariable ( ) .getAnAccess ( ) or
@@ -271,6 +297,15 @@ class PointerFormation extends TPointerFormation {
271297
272298 DataFlow:: Node getBaseNode ( ) { result .asIndirectExpr ( ) = this .getBase ( ) }
273299
300+ /**
301+ * Gets the type of the elements that the base pointer points to. If the base is an array,
302+ * this is the element type of the array. For example, for `a[1][2]` where `a` has type
303+ * `int[2][3]`, this is `int[3]` for `a[1]`, and `int` for `a[1][2]`.
304+ */
305+ Type getBaseElementType ( ) {
306+ result = this .getBase ( ) .getFullyConverted ( ) .getUnspecifiedType ( ) .( PointerType ) .getBaseType ( )
307+ }
308+
274309 /**
275310 * Gets the expression associated with this pointer formation.
276311 */
@@ -328,6 +363,12 @@ class FatPointer extends TFatPointer {
328363 result = this .asIndexAdjusted ( ) .getOffset ( )
329364 }
330365
366+ /**
367+ * Gets the type of the elements that the base pointer of this fat pointer points to, if this
368+ * is an index-adjusted pointer.
369+ */
370+ Type getBaseElementType ( ) { result = this .asIndexAdjusted ( ) .getBaseElementType ( ) }
371+
331372 DataFlow:: Node getNode ( ) {
332373 result = this .asAllocated ( ) .getNode ( ) or
333374 result = this .asIndexAdjusted ( ) .getNode ( )
@@ -389,9 +430,10 @@ class FatPointer extends TFatPointer {
389430 * - currentOffset_0 = srcOffset_0 + sinkOffset_0 if src is an ArrayAllocation
390431 * - currentOffset_n = currentOffset_{n-1} + sinkOffset_n if src is a PointerFormation
391432 *
392- * Note that for n-dimensional stack-initialized arrays, the length information is created for each
393- * initialization level. For example, the below 2-dimensional array produces two `def. of buf` with
394- * length 2 and length 3, respectively.
433+ * Note that n-dimensional stack-initialized arrays have a length for each dimension. The length
434+ * that is used is determined by the type of the base pointer of `end`. For example, the below
435+ * 2-dimensional array has length 2 when it is accessed through a pointer to `int[3]` (as in
436+ * `buf[2]`), and length 3 when it is accessed through a pointer to `int` (as in `buf[0][3]`).
395437 *
396438 * ``` C++
397439 * int buf[2][3] = {{1, 2, 3}, {4, 5, 6}}
@@ -411,7 +453,7 @@ predicate srcSinkLengthMap(
411453 /* Implement the equation that computes the current offset. */
412454 (
413455 /* Base case: The object is allocated and a fat pointer created. */
414- length = start .asAllocated ( ) .getLength ( src . getNode ( ) ) and // Get the length at the given indirection level.
456+ length = start .asAllocated ( ) .getLength ( end . getBaseElementType ( ) ) and
415457 currentOffset = srcOffset + sinkOffset
416458 or
417459 /* Recursive case: A fat pointer is derived from a fat pointer. */
0 commit comments