2 * Copyright (C) 2013 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
24 static void match_assign(const char *fn
, struct expression
*expr
, void *unused
)
26 set_state_expr(my_id
, expr
->left
, &devm
);
29 static void match_free_func(const char *fn
, struct expression
*expr
, void *_arg
)
31 struct expression
*arg_expr
;
32 int arg
= PTR_INT(_arg
);
35 arg_expr
= get_argument_from_call_expr(expr
->args
, arg
);
36 if (!get_state_expr(my_id
, arg_expr
))
38 name
= expr_to_str(arg_expr
);
39 sm_msg("warn: passing devm_ allocated variable to kfree. '%s'", name
);
43 static void register_funcs_from_file(void)
49 token
= get_tokens_file("kernel.frees_argument");
52 if (token_type(token
) != TOKEN_STREAMBEGIN
)
55 while (token_type(token
) != TOKEN_STREAMEND
) {
56 if (token_type(token
) != TOKEN_IDENT
)
58 func
= show_ident(token
->ident
);
60 if (token_type(token
) != TOKEN_NUMBER
)
62 arg
= atoi(token
->number
);
63 add_function_hook(func
, &match_free_func
, INT_PTR(arg
));
69 void check_freeing_devm(int id
)
71 if (option_project
!= PROJ_KERNEL
)
76 add_function_assign_hook("devm_kmalloc", &match_assign
, NULL
);
77 add_function_assign_hook("devm_kzalloc", &match_assign
, NULL
);
78 add_function_assign_hook("devm_kcalloc", &match_assign
, NULL
);
79 add_function_assign_hook("devm_kmalloc_array", &match_assign
, NULL
);
82 add_function_hook("kfree", &match_free_func
, INT_PTR(0));
83 add_function_hook("krealloc", &match_free_func
, INT_PTR(0));
84 register_funcs_from_file();