1313 */
1414
1515import cpp
16+ import semmle.code.cpp.models.Models
1617
17- class Allocation extends FunctionCall {
18- Allocation ( ) { this .getTarget ( ) .hasGlobalOrStdName ( [ "malloc" , "calloc" , "realloc" ] ) }
19-
20- private string getName ( ) { this .getTarget ( ) .hasGlobalOrStdName ( result ) }
21-
22- int getSize ( ) {
23- this .getName ( ) = "malloc" and
24- this .getArgument ( 0 ) .getValue ( ) .toInt ( ) = result
25- or
26- this .getName ( ) = "realloc" and
27- this .getArgument ( 1 ) .getValue ( ) .toInt ( ) = result
28- or
29- this .getName ( ) = "calloc" and
30- result = this .getArgument ( 0 ) .getValue ( ) .toInt ( ) * this .getArgument ( 1 ) .getValue ( ) .toInt ( )
31- }
32- }
33-
34- predicate baseType ( Allocation alloc , Type base ) {
18+ predicate baseType ( AllocationExpr alloc , Type base ) {
3519 exists ( PointerType pointer |
3620 pointer .getBaseType ( ) = base and
3721 (
@@ -49,11 +33,12 @@ predicate decideOnSize(Type t, int size) {
4933 size = min ( t .getSize ( ) )
5034}
5135
52- from Allocation alloc , Type base , int basesize , int allocated
36+ from AllocationExpr alloc , Type base , int basesize , int allocated
5337where
5438 baseType ( alloc , base ) and
55- allocated = alloc .getSize ( ) and
39+ allocated = alloc .getSizeBytes ( ) and
5640 decideOnSize ( base , basesize ) and
41+ alloc .( FunctionCall ) .getTarget ( ) instanceof AllocationFunction and // exclude `new` and similar
5742 // If the codebase has more than one type with the same name, check if any matches
5843 not exists ( int size | base .getSize ( ) = size |
5944 size = 0 or
0 commit comments