db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / check_snprintf.c
blobf49dab492c221ba3b1152a334393046d49868692
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static int my_id;
24 static void match_snprintf(const char *fn, struct expression *expr, void *info)
26 struct expression *call;
27 struct expression *arg;
28 sval_t buflen;
30 call = strip_expr(expr->right);
31 arg = get_argument_from_call_expr(call->args, 1);
32 if (!get_fuzzy_max(arg, &buflen))
33 return;
34 set_state_expr(my_id, expr->left, alloc_state_num(buflen.value));
37 static int get_old_buflen(struct sm_state *sm)
39 struct sm_state *tmp;
40 int ret = 0;
42 FOR_EACH_PTR(sm->possible, tmp) {
43 if (PTR_INT(tmp->state->data) > ret)
44 ret = PTR_INT(tmp->state->data);
45 } END_FOR_EACH_PTR(tmp);
46 return ret;
49 static void match_call(struct expression *expr)
51 struct expression *arg;
52 struct sm_state *sm;
53 int old_buflen;
54 sval_t max;
56 FOR_EACH_PTR(expr->args, arg) {
57 sm = get_sm_state_expr(my_id, arg);
58 if (!sm)
59 continue;
60 old_buflen = get_old_buflen(sm);
61 if (!old_buflen)
62 return;
63 if (get_absolute_max(arg, &max) && sval_cmp_val(max, old_buflen) > 0)
64 sm_warning("'%s' returned from snprintf() might be larger than %d",
65 sm->name, old_buflen);
66 } END_FOR_EACH_PTR(arg);
69 void check_snprintf(int id)
71 if (option_project != PROJ_KERNEL)
72 return;
73 if (!option_spammy)
74 return;
76 my_id = id;
77 set_dynamic_states(my_id);
78 add_hook(&match_call, FUNCTION_CALL_HOOK);
79 add_function_assign_hook("snprintf", &match_snprintf, NULL);
80 add_modification_hook(my_id, &set_undefined);