Print this page
11506 smatch resync
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_shift_to_zero.c
+++ new/usr/src/tools/smatch/src/check_shift_to_zero.c
1 1 /*
2 2 * Copyright (C) 2014 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #include "smatch.h"
19 19
20 20 static int my_id;
21 21
22 22 static void match_binop(struct expression *expr)
23 23 {
24 24 struct symbol *type;
25 25 sval_t bits;
26 26
27 27 if (expr->op != SPECIAL_RIGHTSHIFT)
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
28 28 return;
29 29
30 30 if (!get_implied_value(expr->right, &bits))
31 31 return;
32 32
33 33 type = get_type(expr->left);
34 34 if (!type)
35 35 return;
36 36 if (type_bits(type) == -1 || type_bits(type) > bits.value)
37 37 return;
38 + if (is_ignored_expr(my_id, expr))
39 + return;
38 40 sm_warning("right shifting more than type allows %d vs %lld", type_bits(type), bits.value);
39 41 }
40 42
41 43 static void match_binop2(struct expression *expr)
42 44 {
43 45 struct expression *left;
44 46 struct expression *tmp;
45 47 sval_t mask, shift;
46 48
47 49 if (expr->op != SPECIAL_RIGHTSHIFT)
48 50 return;
49 51
50 52 left = strip_expr(expr->left);
51 53 tmp = get_assigned_expr(left);
52 54 if (tmp)
53 55 left = tmp;
54 56 if (left->type != EXPR_BINOP || left->op != '&')
55 57 return;
56 58
57 59 if (!get_implied_value(expr->right, &shift))
58 60 return;
59 61 if (!get_implied_value(left->right, &mask))
60 62 return;
61 63
62 64 if (mask.uvalue >> shift.uvalue)
63 65 return;
64 66
65 67 sm_warning("mask and shift to zero");
66 68 }
67 69
68 70 static void match_assign(struct expression *expr)
69 71 {
70 72 struct symbol *type;
71 73 sval_t bits;
72 74
73 75 if (expr->op != SPECIAL_SHR_ASSIGN)
74 76 return;
75 77
76 78 if (!get_implied_value(expr->right, &bits))
77 79 return;
78 80 type = get_type(expr->left);
79 81 if (!type)
80 82 return;
81 83 if (type_bits(type) > bits.value)
82 84 return;
83 85 sm_warning("right shift assign to zero");
84 86 }
85 87
86 88 void check_shift_to_zero(int id)
87 89 {
88 90 my_id = id;
89 91
90 92 add_hook(&match_binop, BINOP_HOOK);
91 93 add_hook(&match_binop2, BINOP_HOOK);
92 94
93 95 add_hook(&match_assign, ASSIGNMENT_HOOK);
94 96
95 97 }
↓ open down ↓ |
48 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX