-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_bogus_loop.c
121 lines (103 loc) · 2.82 KB
/
check_bogus_loop.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
/*
* Copyright (C) 2011 Dan Carpenter.
*
* 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"
static int my_id;
static int right_side_changes(struct expression *expr)
{
sval_t dummy;
if (get_value(expr->right, &dummy))
return 0;
return 1;
}
static bool is_lt_ARRAY_SIZE(struct expression *expr)
{
char *macro;
/*
* One cause of false positives is:
* for (i = 0; i < ARRAY_SIZE(); i++) {
* but the ARRAY_SIZE() is zero. Silence these false positives.
*
*/
if (!expr || expr->type != EXPR_COMPARE)
return false;
if (expr->op != '<' && expr->op != SPECIAL_UNSIGNED_LT)
return false;
if (!expr_is_zero(expr->right))
return false;
macro = get_macro_name(expr->right->pos);
if (!macro || strcmp(macro, "ARRAY_SIZE") != 0)
return false;
return true;
}
static struct expression *get_iterator_set(struct statement *stmt)
{
struct expression *expr;
if (!stmt)
return NULL;
if (stmt->type != STMT_EXPRESSION)
return NULL;
expr = stmt->expression;
if (expr->type != EXPR_ASSIGNMENT)
return NULL;
if (expr->op != '=')
return NULL;
if (right_side_changes(expr))
return NULL;
return expr->left;
}
static struct expression *get_iterator_tested(struct expression *expr)
{
if (!expr)
return NULL;
if (expr->type != EXPR_COMPARE)
return NULL;
return expr->left;
}
static void match_loop(struct statement *stmt)
{
struct expression *iterator;
char *iter_set;
char *iter_tested;
if (get_macro_name(stmt->pos))
return;
iterator = get_iterator_set(stmt->iterator_pre_statement);
iter_set = expr_to_var(iterator);
iterator = get_iterator_tested(stmt->iterator_pre_condition);
iter_tested = expr_to_var(iterator);
if (!iter_set || !iter_tested)
goto free;
if (strcmp(iter_set, iter_tested))
goto free;
/* smatch doesn't handle loops correctly so this silences some
* false positives.
*/
if (right_side_changes(stmt->iterator_pre_condition))
goto free;
if (is_lt_ARRAY_SIZE(stmt->iterator_pre_condition))
goto free;
if (implied_condition_false(stmt->iterator_pre_condition))
sm_warning("we never enter this loop");
free:
free_string(iter_set);
free_string(iter_tested);
}
void check_bogus_loop(int id)
{
my_id = id;
add_hook(&match_loop, PRELOOP_HOOK);
}