param_key: fix container of when no struct member is referenced
[smatch.git] / check_freeing_devm.c
blob51fa990f1b4a789bbad980951fcdee71b51b01df
1 /*
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
18 #include "smatch.h"
20 STATE(devm);
22 static int my_id;
24 static void match_assign(const char *fn, struct expression *expr, void *unused)
26 set_state_expr(my_id, expr->left, &devm);
30 * This hook deals with things like:
31 * ptr = devm_kmalloc(...);
32 * if (!ptr)
33 * return -ENOMEM;
34 * another_val = ptr; <==
36 static void match_reassign(struct expression *expr)
38 struct expression *left, *right;
40 if (expr->op != '=')
41 return;
43 right = strip_expr(expr->right);
44 if (!get_state_expr(my_id, right))
45 return;
47 left = strip_expr(expr->left);
49 set_state_expr(my_id, left, &devm);
52 static void match_free_func(const char *fn, struct expression *expr, void *_arg)
54 struct expression *arg_expr;
55 int arg = PTR_INT(_arg);
56 char *name;
58 arg_expr = get_argument_from_call_expr(expr->args, arg);
59 if (!get_state_expr(my_id, arg_expr))
60 return;
61 name = expr_to_str(arg_expr);
62 sm_warning("passing devm_ allocated variable to %s. '%s'", fn, name);
63 free_string(name);
66 static void register_funcs_from_file(void)
68 struct token *token;
69 const char *func;
70 int arg;
72 token = get_tokens_file("kernel.frees_argument");
73 if (!token)
74 return;
75 if (token_type(token) != TOKEN_STREAMBEGIN)
76 return;
77 token = token->next;
78 while (token_type(token) != TOKEN_STREAMEND) {
79 if (token_type(token) != TOKEN_IDENT)
80 return;
81 func = show_ident(token->ident);
82 token = token->next;
83 if (token_type(token) != TOKEN_NUMBER)
84 return;
85 arg = atoi(token->number);
86 add_function_hook(func, &match_free_func, INT_PTR(arg));
87 token = token->next;
89 clear_token_alloc();
92 void check_freeing_devm(int id)
94 if (option_project != PROJ_KERNEL)
95 return;
97 my_id = id;
99 add_function_assign_hook("devm_kmalloc", &match_assign, NULL);
100 add_function_assign_hook("devm_kzalloc", &match_assign, NULL);
101 add_function_assign_hook("devm_kcalloc", &match_assign, NULL);
102 add_function_assign_hook("devm_kmalloc_array", &match_assign, NULL);
104 add_function_assign_hook("devm_kmemdup", &match_assign, NULL);
105 add_function_assign_hook("devm_kstrdup", &match_assign, NULL);
106 add_function_assign_hook("devm_kasprintf", &match_assign, NULL);
107 add_function_assign_hook("devm_kvasprintf", &match_assign, NULL);
109 add_function_hook("kfree", &match_free_func, INT_PTR(0));
110 add_function_hook("krealloc", &match_free_func, INT_PTR(0));
111 register_funcs_from_file();
113 add_hook(&match_reassign, ASSIGNMENT_HOOK);