2 * smatch/check_kmalloc_to_bugon.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 extern int check_assigned_expr_id
;
16 static int is_kmalloc_call(struct expression
*expr
)
18 if (expr
->type
!= EXPR_CALL
)
20 if (expr
->fn
->type
!= EXPR_SYMBOL
)
22 if (!strcmp(expr
->fn
->symbol_name
->name
, "kmalloc"))
24 if (!strcmp(expr
->fn
->symbol_name
->name
, "kzalloc"))
29 static void match_condition(struct expression
*expr
)
32 struct smatch_state
*state
;
33 struct expression
*right
;
36 macro
= get_macro_name(expr
->pos
);
37 if (!macro
|| strcmp(macro
, "BUG_ON") != 0)
39 state
= get_state_expr(check_assigned_expr_id
, expr
);
40 if (!state
|| !state
->data
)
42 right
= (struct expression
*)state
->data
;
43 if (!is_kmalloc_call(right
))
46 name
= get_variable_from_expr(expr
, NULL
);
47 sm_msg("warn: bug on allocation failure '%s'", name
);
51 void check_kmalloc_to_bugon(int id
)
53 if (option_project
!= PROJ_KERNEL
)
58 add_hook(&match_condition
, CONDITION_HOOK
);