@@ -271,33 +271,32 @@ class OperatorNewAllocationFunction extends AllocationFunction {
271
271
}
272
272
273
273
/**
274
- * The predicate analyzes a `sizeExpr`, which is an argument to an allocation
275
- * function like malloc, and tries to split it into an expression `lengthExpr`
276
- * that describes the length of the allocated array, and the size of the allocated
277
- * element type `sizeof`.
278
- * If this is not possible, the allocation is considered to be of size 1 and of
279
- * length `sizeExpr`.
274
+ * Holds if `sizeExpr` is an expression consisting of a subexpression
275
+ * `lengthExpr` multiplied by a constant `sizeof` that is the result of a
276
+ * `sizeof()` expression. Alternatively if there isn't a suitable `sizeof()`
277
+ * expression, `lengthExpr = sizeExpr` and `sizeof = 1`. For example:
278
+ * ```
279
+ * malloc(a * 2 * sizeof(char32_t));
280
+ * ```
281
+ * In this case if the `sizeExpr` is the argument to `malloc`, the `lengthExpr`
282
+ * is `a * 2` and `sizeof` is `4`.
280
283
*/
281
284
private predicate deconstructSizeExpr ( Expr sizeExpr , Expr lengthExpr , int sizeof ) {
282
- if
283
- sizeExpr instanceof MulExpr and
284
- exists ( SizeofOperator sizeofOp , Expr lengthOp |
285
- sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
286
- lengthOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
287
- not lengthOp instanceof SizeofOperator and
288
- exists ( sizeofOp .getValue ( ) .toInt ( ) )
289
- )
290
- then
291
- exists ( SizeofOperator sizeofOp |
292
- sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
293
- lengthExpr = sizeExpr .( MulExpr ) .getAnOperand ( ) and
294
- not lengthExpr instanceof SizeofOperator and
295
- sizeof = sizeofOp .getValue ( ) .toInt ( )
296
- )
297
- else (
298
- lengthExpr = sizeExpr and
299
- sizeof = 1
285
+ exists ( SizeofOperator sizeofOp |
286
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
287
+ lengthExpr = sizeExpr .( MulExpr ) .getAnOperand ( ) and
288
+ not lengthExpr instanceof SizeofOperator and
289
+ sizeof = sizeofOp .getValue ( ) .toInt ( )
300
290
)
291
+ or
292
+ not exists ( SizeofOperator sizeofOp , Expr lengthOp |
293
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
294
+ lengthOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
295
+ not lengthOp instanceof SizeofOperator and
296
+ exists ( sizeofOp .getValue ( ) .toInt ( ) )
297
+ ) and
298
+ lengthExpr = sizeExpr and
299
+ sizeof = 1
301
300
}
302
301
303
302
/**
0 commit comments