-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsmatch_strings.c
162 lines (131 loc) · 3.87 KB
/
smatch_strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (C) 2015 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int my_id;
static int get_str(void *_ret, int argc, char **argv, char **azColName)
{
char **ret = _ret;
if (*ret)
*ret = (void *)-1UL;
else
*ret = alloc_sname(argv[0]);
return 0;
}
static char *get_string_from_mtag(mtag_t tag)
{
char *str = NULL;
run_sql(get_str, &str,
"select value from mtag_data where tag = %lld and offset = 0 and type = %d;",
tag, STRING_VALUE);
if ((unsigned long)str == -1UL)
return NULL;
return str;
}
struct expression *fake_string_from_mtag(mtag_t tag)
{
char *str;
if (!tag)
return NULL;
str = get_string_from_mtag(tag);
if (!str)
return NULL;
return gen_string_expression(str);
}
static void match_strcpy(const char *fn, struct expression *expr, void *unused)
{
struct expression *dest, *src;
dest = get_argument_from_call_expr(expr->args, 0);
src = get_argument_from_call_expr(expr->args, 1);
src = strip_expr(src);
if (src->type == EXPR_STRING)
set_state_expr(my_id, dest, alloc_state_str(src->string->data));
}
struct state_list *get_strings(struct expression *expr)
{
struct state_list *ret = NULL;
struct smatch_state *state;
struct sm_state *sm;
expr = strip_expr(expr);
if (expr->type == EXPR_STRING) {
state = alloc_state_str(expr->string->data);
sm = alloc_sm_state(my_id, expr->string->data, NULL, state);
add_ptr_list(&ret, sm);
return ret;
}
if (expr->type == EXPR_CONDITIONAL ||
expr->type == EXPR_SELECT) {
struct state_list *true_strings = NULL;
struct state_list *false_strings = NULL;
if (known_condition_true(expr->conditional)) {
if (expr->cond_true)
return get_strings(expr->cond_true);
return get_strings(expr->conditional);
}
if (known_condition_false(expr->conditional))
return get_strings(expr->cond_false);
if (expr->cond_true)
true_strings = get_strings(expr->cond_true);
else
true_strings = get_strings(expr->conditional);
false_strings = get_strings(expr->cond_false);
concat_ptr_list((struct ptr_list *)true_strings, (struct ptr_list **)&false_strings);
free_slist(&true_strings);
return false_strings;
}
sm = get_sm_state_expr(my_id, expr);
if (!sm)
return NULL;
return clone_slist(sm->possible);
}
static void match_assignment(struct expression *expr)
{
struct state_list *slist;
struct sm_state *sm;
if (expr->op != '=')
return;
slist = get_strings(strip_expr(expr->right));
if (!slist)
return;
if (ptr_list_size((struct ptr_list *)slist) == 1) {
sm = first_ptr_list((struct ptr_list *)slist);
set_state_expr(my_id, expr->left, sm->state);
return;
}
}
static void match_string(struct expression *expr)
{
mtag_t tag;
if (expr->type != EXPR_STRING)
return;
if (expr->string->length > 255)
return;
if (!get_string_mtag(expr, &tag))
return;
cache_sql(NULL, NULL, "insert or ignore into mtag_data values (%lld, %d, %d, '%q');",
tag, 0, STRING_VALUE, escape_newlines(expr->string->data));
}
void register_strings(int id)
{
my_id = id;
add_function_hook("strcpy", &match_strcpy, NULL);
add_function_hook("strlcpy", &match_strcpy, NULL);
add_function_hook("strncpy", &match_strcpy, NULL);
add_hook(&match_assignment, ASSIGNMENT_HOOK);
add_hook(&match_string, STRING_HOOK);
}