@@ -255,21 +255,34 @@ class OperatorNewAllocationFunction extends AllocationFunction {
255
255
}
256
256
}
257
257
258
+ /**
259
+ * The predicate analyzes a `sizeExpr`, which is an argument to an allocation
260
+ * function like malloc, and tries to split it into an expression `lengthExpr`
261
+ * that describes the length of the allocated array, and the size of the allocated
262
+ * element type `sizeof`.
263
+ * If this is not possible, the allocation is considered to be of size 1 and of
264
+ * length `sizeExpr`.
265
+ */
258
266
private predicate deconstructSizeExpr ( Expr sizeExpr , Expr lengthExpr , int sizeof ) {
259
- sizeExpr instanceof MulExpr and
260
- exists ( SizeofOperator sizeofOp |
261
- sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
262
- lengthExpr = sizeExpr .( MulExpr ) .getAnOperand ( ) and
263
- not lengthExpr instanceof SizeofOperator and
264
- sizeof = sizeofOp .getValue ( ) .toInt ( )
267
+ if
268
+ sizeExpr instanceof MulExpr and
269
+ exists ( SizeofOperator sizeofOp , Expr lengthOp |
270
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
271
+ lengthOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
272
+ not lengthOp instanceof SizeofOperator and
273
+ exists ( sizeofOp .getValue ( ) .toInt ( ) )
274
+ )
275
+ then
276
+ exists ( SizeofOperator sizeofOp |
277
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
278
+ lengthExpr = sizeExpr .( MulExpr ) .getAnOperand ( ) and
279
+ not lengthExpr instanceof SizeofOperator and
280
+ sizeof = sizeofOp .getValue ( ) .toInt ( )
281
+ )
282
+ else (
283
+ lengthExpr = sizeExpr and
284
+ sizeof = 1
265
285
)
266
- or
267
- not exists ( int s , SizeofOperator sizeofOp |
268
- sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
269
- s = sizeofOp .( SizeofOperator ) .getValue ( ) .toInt ( )
270
- ) and
271
- lengthExpr = sizeExpr and
272
- sizeof = 1
273
286
}
274
287
275
288
/**
@@ -293,15 +306,11 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
293
306
exists ( Expr sizeExpr | sizeExpr = getArgument ( target .getSizeArg ( ) ) |
294
307
if exists ( target .getSizeMult ( ) )
295
308
then result = sizeExpr
296
- else (
309
+ else
297
310
exists ( Expr lengthExpr |
298
311
deconstructSizeExpr ( sizeExpr , lengthExpr , _) and
299
312
result = lengthExpr
300
313
)
301
- or
302
- not exists ( Expr lengthExpr | deconstructSizeExpr ( sizeExpr , lengthExpr , _) ) and
303
- result = sizeExpr
304
- )
305
314
)
306
315
}
307
316
0 commit comments