sizeof_param: fix false positives with strlcpy
[smatch.git] / smatch_local_values.c
blob9a354d4f66f62b96010ef90e2619adc6cbc95f67
1 /*
2 * smatch/local_values.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_slist.h"
13 #include "smatch_extra.h"
15 static int my_id;
18 * I'm going to store the states of local data at the end of each function.
19 * Then at the end of the file, I'll combine the possible range lists for
20 * each state and store the value in the on-disk database.
22 * One issue is that when I read the data back from the in-memory database at
23 * the end of the file, then I don't have access to type information. I'll just
24 * cast everything to "long long" for now, I guess. We'll see how that works.
27 static char *db_vals;
28 static int get_vals(void *unused, int argc, char **argv, char **azColName)
30 db_vals = alloc_string(argv[0]);
31 return 0;
34 int get_local_rl(struct expression *expr, struct range_list **rl)
36 char *name;
37 struct range_list *tmp;
39 if (!is_static(expr))
40 return 0;
41 name = expr_to_var(expr);
42 if (!name)
43 return 0;
45 db_vals = NULL;
46 run_sql(get_vals,
47 "select value from local_values where file = '%s' and variable = '%s'",
48 get_filename(), name);
49 free_string(name);
50 if (!db_vals)
51 return 0;
52 str_to_rl(&llong_ctype, db_vals, &tmp);
53 *rl = cast_rl(get_type(expr), tmp);
54 free_string(db_vals);
56 return 1;
59 int get_local_max_helper(struct expression *expr, sval_t *sval)
61 struct range_list *rl;
63 if (!get_local_rl(expr, &rl))
64 return 0;
65 *sval = rl_max(rl);
66 return 1;
69 int get_local_min_helper(struct expression *expr, sval_t *sval)
71 struct range_list *rl;
73 if (!get_local_rl(expr, &rl))
74 return 0;
75 *sval = rl_min(rl);
76 return 1;
79 static struct smatch_state *unmatched_state(struct sm_state *sm)
81 return alloc_estate_empty();
84 static void extra_mod_hook(const char *name, struct symbol *sym, struct smatch_state *state)
86 struct smatch_state *old;
87 struct smatch_state *new;
89 if (!(sym->ctype.modifiers & MOD_STATIC))
90 return;
91 old = get_state(my_id, name, sym);
92 if (old)
93 new = merge_estates(old, state);
94 else
95 new = state;
96 set_state(my_id, name, sym, new);
99 static void process_states(struct state_list *slist)
101 struct sm_state *sm;
102 struct range_list *rl;
104 FOR_EACH_PTR(slist, sm) {
105 if (sm->owner != my_id)
106 continue;
107 rl = cast_rl(&llong_ctype, estate_rl(sm->state));
108 mem_sql(NULL,
109 "insert into local_values values ('%s', '%s', '%s', %lu);",
110 get_filename(), sm->name, show_rl(rl),
111 (unsigned long)sm->sym);
112 } END_FOR_EACH_PTR(sm);
115 static int get_initial_value_sym(struct symbol *sym, char *name, sval_t *sval)
117 struct expression *expr_symbol, *deref, *tmp;
118 char *member_name;
120 if (!sym)
121 return 0;
123 if (!sym->initializer) {
124 *sval = sval_type_val(&llong_ctype, 0);
125 return 1;
127 if (sym->initializer->type != EXPR_INITIALIZER)
128 return get_value(sym->initializer, sval);
130 expr_symbol = symbol_expression(sym);
131 FOR_EACH_PTR(sym->initializer->expr_list, tmp) {
132 if (tmp->type != EXPR_IDENTIFIER) /* how to handle arrays?? */
133 continue;
134 deref = member_expression(expr_symbol, '.', tmp->expr_ident);
135 member_name = expr_to_var(deref);
136 if (!member_name)
137 continue;
138 if (strcmp(name, member_name) == 0) {
139 free_string(member_name);
140 return get_value(tmp->ident_expression, sval);
142 free_string(member_name);
143 } END_FOR_EACH_PTR(tmp);
145 return 0;
148 static char *cur_name;
149 static struct symbol *cur_symbol;
150 static struct range_list *cur_rl;
151 static void add_current_local(void)
153 sval_t initial;
155 if (!get_initial_value_sym(cur_symbol, cur_name, &initial)) {
156 free_string(cur_name);
157 cur_name = NULL;
158 cur_rl = NULL;
159 return;
161 add_range(&cur_rl, initial, initial);
162 if (!is_whole_rl(cur_rl))
163 sql_insert_local_values(cur_name, show_rl(cur_rl));
164 free_string(cur_name);
165 cur_name = NULL;
166 cur_rl = NULL;
169 static int save_final_values(void *unused, int argc, char **argv, char **azColName)
171 char *name = argv[0];
172 char *sym_str = argv[1];
173 char *value = argv[2];
174 struct range_list *rl;
176 cur_symbol = (struct symbol *)strtoul(sym_str, NULL, 10);
178 if (!cur_name)
179 cur_name = alloc_string(name);
180 else if (strcmp(cur_name, name) != 0) {
181 add_current_local();
182 cur_name = alloc_string(name);
183 cur_rl = NULL;
186 str_to_rl(&llong_ctype, value, &rl);
187 cur_rl = rl_union(cur_rl, rl);
189 return 0;
192 static void match_end_file(void)
194 mem_sql(save_final_values,
195 "select distinct variable, symbol, value from local_values order by variable;");
196 if (cur_name)
197 add_current_local();
200 void register_local_values(int id)
202 my_id = id;
204 if (option_info) {
205 add_extra_mod_hook(&extra_mod_hook);
206 add_unmatched_state_hook(my_id, &unmatched_state);
207 add_merge_hook(my_id, &merge_estates);
208 all_return_states_hook(&process_states);
209 add_hook(match_end_file, END_FILE_HOOK);
210 mem_sql(NULL, "alter table local_values add column symbol integer;");