math: array parameters can be NULL
[smatch.git] / check_bogus_loop.c
blob44d5ace25c2516c0f5f41f40dfe1f2b131ec6d4c
1 /*
2 * Copyright (C) 2011 Dan Carpenter.
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
21 static int my_id;
23 static int right_side_changes(struct expression *expr)
25 sval_t dummy;
27 if (get_value(expr->right, &dummy))
28 return 0;
29 return 1;
32 static struct expression *get_iterator_set(struct statement *stmt)
34 struct expression *expr;
36 if (!stmt)
37 return NULL;
38 if (stmt->type != STMT_EXPRESSION)
39 return NULL;
40 expr = stmt->expression;
41 if (expr->type != EXPR_ASSIGNMENT)
42 return NULL;
43 if (expr->op != '=')
44 return NULL;
45 if (right_side_changes(expr))
46 return NULL;
47 return expr->left;
50 static struct expression *get_iterator_tested(struct expression *expr)
52 if (!expr)
53 return NULL;
54 if (expr->type != EXPR_COMPARE)
55 return NULL;
56 return expr->left;
59 static void match_loop(struct statement *stmt)
61 struct expression *iterator;
62 char *iter_set;
63 char *iter_tested;
65 if (get_macro_name(stmt->pos))
66 return;
68 iterator = get_iterator_set(stmt->iterator_pre_statement);
69 iter_set = expr_to_var(iterator);
70 iterator = get_iterator_tested(stmt->iterator_pre_condition);
71 iter_tested = expr_to_var(iterator);
72 if (!iter_set || !iter_tested)
73 goto free;
74 if (strcmp(iter_set, iter_tested))
75 goto free;
77 /* smatch doesn't handle loops correctly so this silences some
78 * false positives.
80 if (right_side_changes(stmt->iterator_pre_condition))
81 goto free;
83 if (implied_condition_false(stmt->iterator_pre_condition))
84 sm_warning("we never enter this loop");
86 free:
87 free_string(iter_set);
88 free_string(iter_tested);
91 void check_bogus_loop(int id)
93 my_id = id;
94 add_hook(&match_loop, PRELOOP_HOOK);