flow: fix struct initialization bug
[smatch.git] / smatch_common_functions.c
blob8788a5df00b6519a7bd1306b26714087553db04d
1 /*
2 * smatch/smatch_common_functions.c
4 * Copyright (C) 2013 Oracle.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "scope.h"
11 #include "smatch.h"
12 #include "smatch_extra.h"
14 static int match_strlen(struct expression *call, void *unused, struct range_list **rl)
16 struct expression *str;
17 unsigned long max;
19 str = get_argument_from_call_expr(call->args, 0);
20 if (get_implied_strlen(str, rl) && sval_is_positive(rl_min(*rl))) {
21 *rl = cast_rl(&ulong_ctype, *rl);
22 return 1;
24 /* smatch_strlen.c is not very complete */
25 max = get_array_size_bytes_max(str);
26 if (max == 0) {
27 *rl = alloc_whole_rl(&ulong_ctype);
28 } else {
29 max--;
30 *rl = alloc_rl(ll_to_sval(0), ll_to_sval(max));
32 return 1;
35 static int match_strnlen(struct expression *call, void *unused, struct range_list **rl)
37 struct expression *limit;
38 sval_t fixed;
39 sval_t bound;
40 sval_t ulong_max = sval_type_val(&ulong_ctype, ULONG_MAX);
42 match_strlen(call, NULL, rl);
43 limit = get_argument_from_call_expr(call->args, 1);
44 if (!get_implied_max(limit, &bound))
45 return 1;
46 if (sval_cmp(bound, ulong_max) == 0)
47 return 1;
48 if (rl_to_sval(*rl, &fixed) && sval_cmp(fixed, bound) >= 0) {
49 *rl = alloc_rl(bound, bound);
50 return 1;
53 bound.value++;
54 *rl = remove_range(*rl, bound, ulong_max);
56 return 1;
59 void register_common_functions(int id)
61 add_implied_return_hook("strlen", &match_strlen, NULL);
62 add_implied_return_hook("strnlen", &match_strnlen, NULL);