@@ -255,6 +255,24 @@ class OperatorNewAllocationFunction extends AllocationFunction {
255
255
}
256
256
}
257
257
258
+ pragma [ inline]
259
+ private predicate deconstructSizeExpr ( Expr sizeExpr , Expr lengthExpr , int sizeof ) {
260
+ sizeExpr instanceof MulExpr and
261
+ exists ( SizeofOperator sizeofOp |
262
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
263
+ lengthExpr = sizeExpr .( MulExpr ) .getAnOperand ( ) and
264
+ sizeofOp != lengthExpr and
265
+ sizeof = sizeofOp .getValue ( ) .toInt ( )
266
+ )
267
+ or
268
+ not exists ( int s , SizeofOperator sizeofOp |
269
+ sizeofOp = sizeExpr .( MulExpr ) .getAnOperand ( ) and
270
+ s = sizeofOp .( SizeofOperator ) .getValue ( ) .toInt ( )
271
+ ) and
272
+ lengthExpr = sizeExpr and
273
+ sizeof = 1
274
+ }
275
+
258
276
/**
259
277
* An allocation expression that is a function call, such as call to `malloc`.
260
278
*/
@@ -272,15 +290,29 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
272
290
not exists ( NewOrNewArrayExpr new | new .getAllocatorCall ( ) = this )
273
291
}
274
292
275
- override Expr getSizeExpr ( ) { result = getArgument ( target .getSizeArg ( ) ) }
293
+ override Expr getSizeExpr ( ) {
294
+ exists ( Expr sizeExpr | sizeExpr = getArgument ( target .getSizeArg ( ) ) |
295
+ if exists ( target .getSizeMult ( ) )
296
+ then result = sizeExpr
297
+ else (
298
+ exists ( Expr lengthExpr |
299
+ deconstructSizeExpr ( sizeExpr , lengthExpr , _) and
300
+ result = lengthExpr
301
+ )
302
+ or
303
+ not exists ( Expr lengthExpr | deconstructSizeExpr ( sizeExpr , lengthExpr , _) ) and
304
+ result = sizeExpr
305
+ )
306
+ )
307
+ }
276
308
277
309
override int getSizeMult ( ) {
278
310
// malloc with multiplier argument that is a constant
279
311
result = getArgument ( target .getSizeMult ( ) ) .getValue ( ) .toInt ( )
280
312
or
281
313
// malloc with no multiplier argument
282
314
not exists ( target .getSizeMult ( ) ) and
283
- result = 1
315
+ deconstructSizeExpr ( getArgument ( target . getSizeArg ( ) ) , _ , result )
284
316
}
285
317
286
318
override int getSizeBytes ( ) { result = getSizeExpr ( ) .getValue ( ) .toInt ( ) * getSizeMult ( ) }
0 commit comments