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 #include "smatch_slist.h"
20 #include "smatch_extra.h"
24 static void match_snprintf(const char *fn
, struct expression
*expr
, void *info
)
26 struct expression
*call
;
27 struct expression
*arg
;
30 call
= strip_expr(expr
->right
);
31 arg
= get_argument_from_call_expr(call
->args
, 1);
32 if (!get_fuzzy_max(arg
, &buflen
))
34 set_state_expr(my_id
, expr
->left
, alloc_state_num(buflen
.value
));
37 static int get_old_buflen(struct sm_state
*sm
)
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
);
49 static void match_call(struct expression
*expr
)
51 struct expression
*arg
;
56 FOR_EACH_PTR(expr
->args
, arg
) {
57 sm
= get_sm_state_expr(my_id
, arg
);
60 old_buflen
= get_old_buflen(sm
);
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
)
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
);