2 * smatch/check_resource_size.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
13 extern int check_assigned_expr_id
;
15 static int is_probably_ok(struct expression
*expr
)
17 expr
= strip_expr(expr
);
19 if (expr
->type
== EXPR_BINOP
)
21 if (expr
->type
== EXPR_SIZEOF
)
27 static void verify_size_expr(struct expression
*expr
)
29 if (expr
->type
!= EXPR_BINOP
)
33 if (is_probably_ok(expr
->left
))
35 if (is_probably_ok(expr
->right
))
37 sm_msg("warn: consider using resource_size() here");
40 static void handle_assigned_expr(struct expression
*expr
)
42 struct smatch_state
*state
;
44 state
= get_state_expr(check_assigned_expr_id
, expr
);
45 if (!state
|| !state
->data
)
47 expr
= (struct expression
*)state
->data
;
48 verify_size_expr(expr
);
51 static void match_resource(const char *fn
, struct expression
*expr
, void *_arg_no
)
53 struct expression
*arg_expr
;
54 int arg_no
= PTR_INT(_arg_no
);
56 arg_expr
= get_argument_from_call_expr(expr
->args
, arg_no
);
57 arg_expr
= strip_expr(arg_expr
);
61 if (arg_expr
->type
== EXPR_SYMBOL
) {
62 handle_assigned_expr(arg_expr
);
65 verify_size_expr(arg_expr
);
68 void check_resource_size(int id
)
72 if (option_project
!= PROJ_KERNEL
)
75 add_function_hook("ioremap_nocache", &match_resource
, (void *)1);
76 add_function_hook("ioremap", &match_resource
, (void *)1);
77 add_function_hook("__request_region", &match_resource
, (void *)2);
78 add_function_hook("__release_region", &match_resource
, (void *)2);
79 add_function_hook("__devm_request_region", &match_resource
, (void *)3);
80 add_function_hook("__devm_release_region", &match_resource
, (void *)3);