2 * smatch/smatch_common_functions.c
4 * Copyright (C) 2013 Oracle.
6 * Licensed under the Open Software License version 1.1
12 #include "smatch_extra.h"
14 static int match_strlen(struct expression
*call
, void *unused
, struct range_list
**rl
)
16 struct expression
*str
;
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
);
24 /* smatch_strlen.c is not very complete */
25 max
= get_array_size_bytes_max(str
);
27 *rl
= alloc_whole_rl(&ulong_ctype
);
30 *rl
= alloc_rl(ll_to_sval(0), ll_to_sval(max
));
35 static int match_strnlen(struct expression
*call
, void *unused
, struct range_list
**rl
)
37 struct expression
*limit
;
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
))
46 if (sval_cmp(bound
, ulong_max
) == 0)
48 if (rl_to_sval(*rl
, &fixed
) && sval_cmp(fixed
, bound
) >= 0) {
49 *rl
= alloc_rl(bound
, bound
);
54 *rl
= remove_range(*rl
, bound
, ulong_max
);
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
);