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 ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
26 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
29 static void match_snprintf(const char *fn
, struct expression
*expr
, void *info
)
31 struct expression
*call
;
32 struct expression
*arg
;
35 call
= strip_expr(expr
->right
);
36 arg
= get_argument_from_call_expr(call
->args
, 1);
37 if (!get_fuzzy_max(arg
, &buflen
))
39 set_state_expr(my_id
, expr
->left
, alloc_state_num(buflen
.value
));
42 static int get_old_buflen(struct sm_state
*sm
)
47 FOR_EACH_PTR(sm
->possible
, tmp
) {
48 if (PTR_INT(tmp
->state
->data
) > ret
)
49 ret
= PTR_INT(tmp
->state
->data
);
50 } END_FOR_EACH_PTR(tmp
);
54 static void match_call(struct expression
*expr
)
56 struct expression
*arg
;
61 FOR_EACH_PTR(expr
->args
, arg
) {
62 sm
= get_sm_state_expr(my_id
, arg
);
65 old_buflen
= get_old_buflen(sm
);
68 if (get_absolute_max(arg
, &max
) && sval_cmp_val(max
, old_buflen
) > 0)
69 sm_warning("'%s' returned from snprintf() might be larger than %d",
70 sm
->name
, old_buflen
);
71 } END_FOR_EACH_PTR(arg
);
74 void check_snprintf(int id
)
76 if (option_project
!= PROJ_KERNEL
)
82 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
83 add_function_assign_hook("snprintf", &match_snprintf
, NULL
);
84 add_modification_hook(my_id
, &ok_to_use
);