2 * sparse/check_snprintf.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
16 static void ok_to_use(struct sm_state
*sm
)
18 set_state(my_id
, sm
->name
, sm
->sym
, &undefined
);
21 static void match_snprintf(const char *fn
, struct expression
*expr
, void *info
)
23 struct expression
*call
;
24 struct expression
*arg
;
27 call
= strip_expr(expr
->right
);
28 arg
= get_argument_from_call_expr(call
->args
, 1);
29 if (!get_fuzzy_max(arg
, &buflen
))
31 set_state_expr(my_id
, expr
->left
, alloc_state_num(buflen
.value
));
34 static int get_old_buflen(struct sm_state
*sm
)
39 FOR_EACH_PTR(sm
->possible
, tmp
) {
40 if (PTR_INT(tmp
->state
->data
) > ret
)
41 ret
= PTR_INT(tmp
->state
->data
);
42 } END_FOR_EACH_PTR(tmp
);
46 static void match_call(struct expression
*expr
)
48 struct expression
*arg
;
53 FOR_EACH_PTR(expr
->args
, arg
) {
54 sm
= get_sm_state_expr(my_id
, arg
);
57 old_buflen
= get_old_buflen(sm
);
60 if (get_absolute_max(arg
, &max
) && sval_cmp_val(max
, old_buflen
) > 0)
61 sm_msg("warn: '%s' returned from snprintf() might be larger than %d",
62 sm
->name
, old_buflen
);
63 } END_FOR_EACH_PTR(arg
);
66 void check_snprintf(int id
)
68 if (option_project
!= PROJ_KERNEL
)
74 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
75 add_function_assign_hook("snprintf", &match_snprintf
, NULL
);
76 add_modification_hook(my_id
, &ok_to_use
);