kchecker: build already built directory
[smatch.git] / check_return_efault.c
blob29b8aaddfce241c8ea1e7489113d45057fdb4a51
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
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
19 * This tries to find places which should probably return -EFAULT
20 * but return the number of bytes to copy instead.
23 #include "smatch.h"
24 #include "smatch_slist.h"
25 #include "smatch_extra.h"
27 static int my_id;
29 STATE(remaining);
30 STATE(ok);
32 static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
34 if (sm->state != &ok)
35 set_state(my_id, sm->name, sm->sym, &ok);
38 static void match_copy(const char *fn, struct expression *expr, void *unused)
40 if (expr->op == SPECIAL_SUB_ASSIGN)
41 return;
42 set_state_expr(my_id, expr->left, &remaining);
45 static void match_condition(struct expression *expr)
47 if (!get_state_expr(my_id, expr))
48 return;
49 /* If the variable is zero that's ok */
50 set_true_false_states_expr(my_id, expr, NULL, &ok);
54 * This function is biased in favour of print out errors.
55 * The heuristic to print is:
56 * If we have a potentially positive return from copy_to_user
57 * and there is a possibility that we return negative as well
58 * then complain.
60 static void match_return(struct expression *ret_value)
62 struct smatch_state *state;
63 struct sm_state *sm;
64 sval_t min;
66 sm = get_sm_state_expr(my_id, ret_value);
67 if (!sm)
68 return;
69 if (!slist_has_state(sm->possible, &remaining))
70 return;
71 state = get_state_expr(SMATCH_EXTRA, ret_value);
72 if (!state)
73 return;
74 if (!get_absolute_min(ret_value, &min))
75 return;
76 if (min.value == 0)
77 return;
78 sm_msg("warn: maybe return -EFAULT instead of the bytes remaining?");
81 void check_return_efault(int id)
83 if (option_project != PROJ_KERNEL)
84 return;
86 my_id = id;
87 add_function_assign_hook("copy_to_user", &match_copy, NULL);
88 add_function_assign_hook("__copy_to_user", &match_copy, NULL);
89 add_function_assign_hook("copy_from_user", &match_copy, NULL);
90 add_function_assign_hook("__copy_from_user", &match_copy, NULL);
91 add_function_assign_hook("clear_user", &match_copy, NULL);
92 add_hook(&match_condition, CONDITION_HOOK);
93 add_hook(&match_return, RETURN_HOOK);
94 add_modification_hook(my_id, &ok_to_use);