@@ -255,21 +255,34 @@ class OperatorNewAllocationFunction extends AllocationFunction {
255255 }
256256}
257257
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+ */
258266private 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
265285 )
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
273286}
274287
275288/**
@@ -293,15 +306,11 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
293306 exists ( Expr sizeExpr | sizeExpr = getArgument ( target .getSizeArg ( ) ) |
294307 if exists ( target .getSizeMult ( ) )
295308 then result = sizeExpr
296- else (
309+ else
297310 exists ( Expr lengthExpr |
298311 deconstructSizeExpr ( sizeExpr , lengthExpr , _) and
299312 result = lengthExpr
300313 )
301- or
302- not exists ( Expr lengthExpr | deconstructSizeExpr ( sizeExpr , lengthExpr , _) ) and
303- result = sizeExpr
304- )
305314 )
306315 }
307316
0 commit comments