Avoid triggering a division by zero in the overflow check
[smatch.git] / smatch_sval.c
blobc1911f569e37dadf1c69a3d0c14e1fd629a876b5
1 /*
2 * smatch/smatch_sval.c
4 * Copyright (C) 2012 Oracle.
6 * Licensed under the Open Software License version 1.1
9 * Basically the point of sval is that it can hold both ULLONG_MAX and
10 * LLONG_MIN. If it is an unsigned type then we use sval.uvalue or if it is
11 * signed we use sval.value.
13 * I considered just using one bit to store whether the value was signed vs
14 * unsigned but I think it might help to have the type information so we know
15 * how to do type promotion.
19 #include "smatch.h"
20 #include "smatch_slist.h"
21 #include "smatch_extra.h"
23 __ALLOCATOR(sval_t, "svals", sval);
25 sval_t *sval_alloc(sval_t sval)
27 sval_t *ret;
29 ret = __alloc_sval(0);
30 *ret = sval;
31 return ret;
34 sval_t *sval_alloc_permanent(sval_t sval)
36 sval_t *ret;
38 ret = malloc(sizeof(*ret));
39 *ret = sval;
40 return ret;
43 sval_t sval_blank(struct expression *expr)
45 sval_t ret;
47 ret.type = get_type(expr);
48 if (!ret.type)
49 ret.type = &llong_ctype;
50 ret.value = 123456789;
52 return ret;
55 sval_t sval_type_val(struct symbol *type, long long val)
57 sval_t ret;
59 ret.type = type;
60 ret.value = val;
61 return ret;
64 sval_t sval_from_val(struct expression *expr, long long val)
66 sval_t ret;
68 ret = sval_blank(expr);
69 ret.value = val;
70 ret = sval_cast(get_type(expr), ret);
72 return ret;
75 int sval_unsigned(sval_t sval)
77 return type_unsigned(sval.type);
80 int sval_signed(sval_t sval)
82 return !type_unsigned(sval.type);
85 int sval_bits(sval_t sval)
87 return type_bits(sval.type);
90 int sval_positive_bits(sval_t sval)
92 return type_positive_bits(sval.type);
95 int sval_bits_used(sval_t sval)
97 int i;
99 for (i = 64; i >= 1; i--) {
100 if (sval.uvalue & (1ULL << (i - 1)))
101 return i;
103 return 0;
106 int sval_is_negative(sval_t sval)
108 if (sval_cmp_val(sval, 0) < 0)
109 return 1;
110 return 0;
113 int sval_is_positive(sval_t sval)
115 return !sval_is_negative(sval);
118 int sval_is_min(sval_t sval)
120 sval_t min = sval_type_min(sval.type);
122 if (sval_unsigned(sval)) {
123 if (sval.uvalue == 0)
124 return 1;
125 return 0;
127 /* return true for less than min as well */
128 return (sval.value <= min.value);
131 int sval_is_max(sval_t sval)
133 sval_t max = sval_type_max(sval.type);
135 if (sval_unsigned(sval))
136 return (sval.uvalue >= max.value);
137 return (sval.value >= max.value);
140 int sval_is_a_min(sval_t sval)
142 if (sval_signed(sval) && sval.value == SHRT_MIN)
143 return 1;
144 if (sval_signed(sval) && sval.value == INT_MIN)
145 return 1;
146 if (sval_signed(sval) && sval.value == LLONG_MIN)
147 return 1;
148 return 0;
151 int sval_is_a_max(sval_t sval)
153 if (sval.uvalue == SHRT_MAX)
154 return 1;
155 if (sval.uvalue == INT_MAX)
156 return 1;
157 if (sval.uvalue == LLONG_MAX)
158 return 1;
159 if (sval.uvalue == USHRT_MAX)
160 return 1;
161 if (sval.uvalue == UINT_MAX)
162 return 1;
163 if (sval_unsigned(sval) && sval.uvalue == ULLONG_MAX)
164 return 1;
165 return 0;
169 * Returns -1 if one is smaller, 0 if they are the same and 1 if two is larger.
171 int sval_cmp(sval_t one, sval_t two)
173 struct symbol *type;
175 type = one.type;
176 if (sval_positive_bits(two) > sval_positive_bits(one))
177 type = two.type;
178 if (type_bits(type) < 31)
179 type = &int_ctype;
181 one = sval_cast(type, one);
182 two = sval_cast(type, two);
184 if (type_unsigned(type)) {
185 if (one.uvalue < two.uvalue)
186 return -1;
187 if (one.uvalue == two.uvalue)
188 return 0;
189 return 1;
191 /* fix me handle type promotion and unsigned values */
192 if (one.value < two.value)
193 return -1;
194 if (one.value == two.value)
195 return 0;
196 return 1;
199 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
201 sval_t one_cast, two_cast;
203 one_cast = sval_cast(type, one);
204 two_cast = sval_cast(type, two);
205 return sval_cmp(one_cast, two_cast);
208 int sval_cmp_val(sval_t one, long long val)
210 sval_t sval;
212 sval = sval_type_val(&llong_ctype, val);
213 return sval_cmp(one, sval);
216 sval_t sval_min(sval_t one, sval_t two)
218 if (sval_cmp(one, two) > 0)
219 return two;
220 return one;
223 sval_t sval_max(sval_t one, sval_t two)
225 if (sval_cmp(one, two) < 0)
226 return two;
227 return one;
230 int sval_too_low(struct symbol *type, sval_t sval)
232 if (sval_is_negative(sval) && type_unsigned(type))
233 return 1;
234 if (type_signed(type) && sval_unsigned(sval))
235 return 0;
236 if (sval_cmp(sval, sval_type_min(type)) < 0)
237 return 1;
238 return 0;
241 int sval_too_high(struct symbol *type, sval_t sval)
243 if (sval_is_negative(sval))
244 return 0;
245 if (sval_cmp(sval, sval_type_max(type)) > 0)
246 return 1;
247 return 0;
250 int sval_fits(struct symbol *type, sval_t sval)
252 if (sval_too_low(type, sval))
253 return 0;
254 if (sval_too_high(type, sval))
255 return 0;
256 return 1;
259 sval_t sval_cast(struct symbol *type, sval_t sval)
261 sval_t ret;
263 if (!type)
264 type = &llong_ctype;
266 ret.type = type;
267 switch (sval_bits(ret)) {
268 case 8:
269 if (sval_unsigned(ret))
270 ret.value = (long long)(unsigned char)sval.value;
271 else
272 ret.value = (long long)(char)sval.value;
273 break;
274 case 16:
275 if (sval_unsigned(ret))
276 ret.value = (long long)(unsigned short)sval.value;
277 else
278 ret.value = (long long)(short)sval.value;
279 break;
280 case 32:
281 if (sval_unsigned(ret))
282 ret.value = (long long)(unsigned int)sval.value;
283 else
284 ret.value = (long long)(int)sval.value;
285 break;
286 default:
287 ret.value = sval.value;
289 return ret;
293 sval_t sval_preop(sval_t sval, int op)
295 switch (op) {
296 case '!':
297 sval.value = !sval.value;
298 break;
299 case '~':
300 sval.value = ~sval.value;
301 /* fixme: should probably cast this here */
302 break;
303 case '-':
304 sval.value = -sval.value;
305 break;
307 return sval;
310 static sval_t sval_binop_unsigned(struct symbol *type, sval_t left, int op, sval_t right)
312 sval_t ret;
314 ret.type = type;
315 switch (op) {
316 case '*':
317 ret.uvalue = left.uvalue * right.uvalue;
318 break;
319 case '/':
320 if (right.uvalue == 0) {
321 sm_msg("debug: %s: divide by zero", __func__);
322 ret.uvalue = 123456789;
323 } else {
324 ret.uvalue = left.uvalue / right.uvalue;
326 break;
327 case '+':
328 ret.uvalue = left.uvalue + right.uvalue;
329 break;
330 case '-':
331 ret.uvalue = left.uvalue - right.uvalue;
332 break;
333 case '%':
334 if (right.uvalue == 0) {
335 sm_msg("internal error: %s: MOD by zero", __func__);
336 ret.uvalue = 123456789;
337 } else {
338 ret.uvalue = left.uvalue % right.uvalue;
340 break;
341 case '|':
342 ret.uvalue = left.uvalue | right.uvalue;
343 break;
344 case '&':
345 ret.uvalue = left.uvalue & right.uvalue;
346 break;
347 case SPECIAL_RIGHTSHIFT:
348 ret.uvalue = left.uvalue >> right.uvalue;
349 break;
350 case SPECIAL_LEFTSHIFT:
351 ret.uvalue = left.uvalue << right.uvalue;
352 break;
353 case '^':
354 ret.uvalue = left.uvalue ^ right.uvalue;
355 break;
356 default:
357 sm_msg("internal error: %s: unhandled binop %s", __func__,
358 show_special(op));
359 ret.uvalue = 1234567;
361 return ret;
365 static sval_t sval_binop_signed(struct symbol *type, sval_t left, int op, sval_t right)
367 sval_t ret;
369 ret.type = type;
370 switch (op) {
371 case '*':
372 ret.value = left.value * right.value;
373 break;
374 case '/':
375 if (right.value == 0) {
376 sm_msg("debug: %s: divide by zero", __func__);
377 ret.value = 123456789;
378 } else if (left.value == LLONG_MIN && right.value == -1) {
379 sm_msg("debug: %s: invalid divide LLONG_MIN/-1", __func__);
380 ret.value = 12345678;
381 } else {
382 ret.value = left.value / right.value;
384 break;
385 case '+':
386 ret.value = left.value + right.value;
387 break;
388 case '-':
389 ret.value = left.value - right.value;
390 break;
391 case '%':
392 if (right.value == 0) {
393 sm_msg("internal error: %s: MOD by zero", __func__);
394 ret.value = 123456789;
395 } else {
396 ret.value = left.value % right.value;
398 break;
399 case '|':
400 ret.value = left.value | right.value;
401 break;
402 case '&':
403 ret.value = left.value & right.value;
404 break;
405 case SPECIAL_RIGHTSHIFT:
406 ret.value = left.value >> right.value;
407 break;
408 case SPECIAL_LEFTSHIFT:
409 ret.value = left.value << right.value;
410 break;
411 case '^':
412 ret.value = left.value ^ right.value;
413 break;
414 default:
415 sm_msg("internal error: %s: unhandled binop %s", __func__,
416 show_special(op));
417 ret.value = 1234567;
419 return ret;
422 sval_t sval_binop(sval_t left, int op, sval_t right)
424 struct symbol *type;
425 sval_t ret;
427 type = left.type;
428 if (sval_positive_bits(right) > sval_positive_bits(left))
429 type = right.type;
430 if (type_positive_bits(type) < 31)
431 type = &int_ctype;
433 if (type_unsigned(type))
434 ret = sval_binop_unsigned(type, left, op, right);
435 else
436 ret = sval_binop_signed(type, left, op, right);
437 return ret;
440 int sval_binop_overflows(sval_t left, int op, sval_t right)
442 sval_t max = sval_type_max(left.type);
444 switch (op) {
445 case '+':
446 if (sval_cmp(left, sval_binop(max, '-', right)) > 0)
447 return 1;
448 return 0;
449 case '*':
450 return right.value != 0 &&
451 sval_cmp(left, sval_binop(max, '/', right)) > 0;
453 return 0;
456 const char *sval_to_str(sval_t sval)
458 char buf[30];
460 if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
461 return "u64max";
462 if (sval_unsigned(sval) && sval.value == UINT_MAX)
463 return "u32max";
464 if (sval_unsigned(sval) && sval.value == USHRT_MAX)
465 return "u16max";
467 if (sval_signed(sval) && sval.value == LLONG_MAX)
468 return "s64max";
469 if (sval_signed(sval) && sval.value == INT_MAX)
470 return "s32max";
471 if (sval_signed(sval) && sval.value == SHRT_MAX)
472 return "s16max";
474 if (sval_signed(sval) && sval.value == SHRT_MIN)
475 return "s16min";
476 if (sval_signed(sval) && sval.value == INT_MIN)
477 return "s32min";
478 if (sval_signed(sval) && sval.value == LLONG_MIN)
479 return "s64min";
481 if (sval_unsigned(sval))
482 snprintf(buf, sizeof(buf), "%llu", sval.value);
483 else if (sval.value < 0)
484 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
485 else
486 snprintf(buf, sizeof(buf), "%lld", sval.value);
488 return alloc_sname(buf);
491 const char *sval_to_numstr(sval_t sval)
493 char buf[30];
495 if (sval_unsigned(sval))
496 snprintf(buf, sizeof(buf), "%llu", sval.value);
497 else if (sval.value < 0)
498 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
499 else
500 snprintf(buf, sizeof(buf), "%lld", sval.value);
502 return alloc_sname(buf);
505 sval_t ll_to_sval(long long val)
507 sval_t ret;
509 ret.type = &llong_ctype;
510 ret.value = val;
511 return ret;
514 static void free_svals(struct symbol *sym)
516 clear_sval_alloc();
519 void register_sval(int my_id)
521 add_hook(&free_svals, END_FUNC_HOOK);