2 * Copyright (C) 2015 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static bool is_non_null_array(struct expression
*expr
)
30 type
= get_type(expr
);
31 if (!type
|| type
->type
!= SYM_ARRAY
)
33 if (expr
->type
== EXPR_SYMBOL
)
35 if (implied_not_equal(expr
, 0))
38 /* verify that it's not the first member of the struct */
39 if (expr
->type
!= EXPR_DEREF
|| !expr
->member
)
41 name
= expr_to_var_sym(expr
, &sym
);
45 type
= get_real_base_type(sym
);
46 if (!type
|| type
->type
!= SYM_PTR
)
48 type
= get_real_base_type(type
);
49 if (type
->type
!= SYM_STRUCT
)
53 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
57 if (strcmp(expr
->member
->name
, tmp
->ident
->name
) == 0) {
62 } END_FOR_EACH_PTR(tmp
);
67 int get_member_offset(struct symbol
*type
, char *member_name
)
72 if (type
->type
!= SYM_STRUCT
)
76 FOR_EACH_PTR(type
->symbol_list
, tmp
) {
77 if (!type
->ctype
.attribute
->is_packed
)
78 offset
= ALIGN(offset
, tmp
->ctype
.alignment
);
79 if (tmp
->ident
&& tmp
->ident
->name
&&
80 strcmp(member_name
, tmp
->ident
->name
) == 0) {
83 offset
+= type_bytes(tmp
);
84 } END_FOR_EACH_PTR(tmp
);
88 int get_member_offset_from_deref(struct expression
*expr
)
94 if (expr
->type
!= EXPR_DEREF
) /* hopefully, this doesn't happen */
97 if (expr
->member_offset
>= 0)
98 return expr
->member_offset
;
100 member
= expr
->member
;
101 if (!member
|| !member
->name
)
104 type
= get_type(expr
->deref
);
105 if (!type
|| type
->type
!= SYM_STRUCT
)
108 offset
= get_member_offset(type
, member
->name
);
110 expr
->member_offset
= offset
;
114 static void add_offset_to_min(struct range_list
**rl
, int offset
)
117 struct range_list
*orig
= *rl
;
118 struct range_list
*offset_rl
;
119 struct range_list
*big_rl
;
120 struct range_list
*tmp
;
123 * I don't know. I guess I want to preserve the upper value because
124 * that has no information. Only the lower value is interesting.
129 sval
= rl_min(orig
); /* get the type */
132 offset_rl
= alloc_rl(sval
, sval
);
133 tmp
= rl_binop(orig
, '+', offset_rl
);
136 /* if we actually "know" the max then preserve it. */
137 if (max
.value
< 100000) {
142 big_rl
= alloc_rl(sval
, max
);
144 *rl
= rl_intersection(tmp
, big_rl
);
147 static struct range_list
*where_allocated_rl(struct symbol
*sym
)
149 if (sym
->ctype
.modifiers
& (MOD_TOPLEVEL
| MOD_STATIC
)) {
150 if (sym
->initializer
)
151 return alloc_rl(data_seg_min
, data_seg_max
);
153 return alloc_rl(bss_seg_min
, bss_seg_max
);
155 return alloc_rl(stack_seg_min
, stack_seg_max
);
158 int get_address_rl(struct expression
*expr
, struct range_list
**rl
)
160 expr
= strip_expr(expr
);
164 if (expr
->type
== EXPR_STRING
) {
165 *rl
= alloc_rl(text_seg_min
, text_seg_max
);
169 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '&') {
170 struct expression
*unop
;
172 unop
= strip_expr(expr
->unop
);
173 if (unop
->type
== EXPR_SYMBOL
) {
174 *rl
= where_allocated_rl(unop
->symbol
);
178 if (unop
->type
== EXPR_DEREF
) {
179 int offset
= get_member_offset_from_deref(unop
);
181 unop
= strip_expr(unop
->unop
);
182 if (unop
->type
== EXPR_SYMBOL
) {
183 *rl
= where_allocated_rl(unop
->symbol
);
184 } else if (unop
->type
== EXPR_PREOP
&& unop
->op
== '*') {
185 unop
= strip_expr(unop
->unop
);
186 get_absolute_rl(unop
, rl
);
191 add_offset_to_min(rl
, offset
);
198 if (is_non_null_array(expr
)) {
199 *rl
= alloc_rl(array_min_sval
, array_max_sval
);