@@ -255,6 +255,24 @@ class OperatorNewAllocationFunction extends AllocationFunction {
255255 }
256256}
257257
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+
258276/**
259277 * An allocation expression that is a function call, such as call to `malloc`.
260278 */
@@ -272,15 +290,29 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
272290 not exists ( NewOrNewArrayExpr new | new .getAllocatorCall ( ) = this )
273291 }
274292
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+ }
276308
277309 override int getSizeMult ( ) {
278310 // malloc with multiplier argument that is a constant
279311 result = getArgument ( target .getSizeMult ( ) ) .getValue ( ) .toInt ( )
280312 or
281313 // malloc with no multiplier argument
282314 not exists ( target .getSizeMult ( ) ) and
283- result = 1
315+ deconstructSizeExpr ( getArgument ( target . getSizeArg ( ) ) , _ , result )
284316 }
285317
286318 override int getSizeBytes ( ) { result = getSizeExpr ( ) .getValue ( ) .toInt ( ) * getSizeMult ( ) }
0 commit comments