2 * smatch/smatch_strlen.c
4 * Copyright (C) 2013 Oracle.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
17 static int my_strlen_id
;
19 static void set_strlen_undefined(struct sm_state
*sm
, struct expression
*mod_expr
)
21 set_state(sm
->owner
, sm
->name
, sm
->sym
, &undefined
);
24 static void match_strlen(const char *fn
, struct expression
*expr
, void *unused
)
26 struct expression
*right
;
27 struct expression
*str
;
28 struct expression
*len_expr
;
30 struct smatch_state
*state
;
32 right
= strip_expr(expr
->right
);
33 str
= get_argument_from_call_expr(right
->args
, 0);
34 len_expr
= strip_expr(expr
->left
);
36 len_name
= expr_to_var(len_expr
);
40 state
= __alloc_smatch_state(0);
41 state
->name
= len_name
;
42 state
->data
= len_expr
;
44 set_state_expr(my_strlen_id
, str
, state
);
47 int get_size_from_strlen(struct expression
*expr
)
49 struct smatch_state
*state
;
52 state
= get_state_expr(my_strlen_id
, expr
);
53 if (!state
|| !state
->data
)
55 if (!get_implied_max((struct expression
*)state
->data
, &len
))
59 if (len
.uvalue
> INT_MAX
- 1 || len
.value
< 0)
61 return len
.value
+ 1; /* add one because strlen doesn't include the NULL */
64 void register_strlen(int id
)
67 add_function_assign_hook("strlen", &match_strlen
, NULL
);
68 add_modification_hook(my_strlen_id
, &set_strlen_undefined
);