pointer_math: don't complain if a pointer math operation is within bounds
[smatch.git] / check_pointer_math.c
blobb61a901d52068d925656ca05d665f93ac0af88cb
1 /*
2 * Copyright (C) 2012 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
18 #include "smatch.h"
20 static int my_id;
22 STATE(size_in_bytes);
24 static void set_undefined(struct sm_state *sm, struct expression *mod_expr)
26 if (sm->state == &size_in_bytes)
27 set_state(my_id, sm->name, sm->sym, &undefined);
30 static int is_sizeof(struct expression *expr)
32 return (expr->type == EXPR_SIZEOF);
35 static int is_align(struct expression *expr)
37 char *name;
38 struct expression *outside_expr;
40 /* check that we aren't inside the ALIGN() macro itself */
41 outside_expr = last_ptr_list((struct ptr_list *)big_expression_stack);
42 if (outside_expr && positions_eq(expr->pos, outside_expr->pos))
43 return 0;
45 name = get_macro_name(expr->pos);
46 if (name && strcmp(name, "ALIGN") == 0)
47 return 1;
48 return 0;
51 static int is_size_in_bytes(struct expression *expr)
53 if (is_sizeof(expr))
54 return 1;
56 if (is_align(expr))
57 return 1;
59 if (get_state_expr(my_id, expr) == &size_in_bytes)
60 return 1;
62 return 0;
65 static void match_binop(struct expression *expr)
67 struct symbol *type;
68 char *name;
69 int size;
71 if (expr->op != '+')
72 return;
73 type = get_pointer_type(expr->left);
74 if (!type)
75 return;
76 if (type_bits(type) <= 8) /* ignore void, bool and char pointers*/
77 return;
78 if (!is_size_in_bytes(expr->right))
79 return;
81 /* if we know it's within bounds then don't complain */
82 size = get_array_size(expr->left);
83 if (size) {
84 sval_t max;
86 get_absolute_max(expr->right, &max);
87 if (max.uvalue < size)
88 return;
91 name = expr_to_str(expr->left);
92 sm_msg("warn: potential pointer math issue ('%s' is a %d bit pointer)",
93 name, type_bits(type));
94 free_string(name);
97 static void match_assign(struct expression *expr)
99 if (expr->op != '=')
100 return;
102 if (!is_size_in_bytes(expr->right))
103 return;
104 set_state_expr(my_id, expr->left, &size_in_bytes);
107 static void check_assign(struct expression *expr)
109 struct symbol *type;
110 char *name;
112 if (expr->op != SPECIAL_ADD_ASSIGN && expr->op != SPECIAL_SUB_ASSIGN)
113 return;
115 type = get_pointer_type(expr->left);
116 if (!type)
117 return;
118 if (type_bits(type) == 8 || type_bits(type) == -1)
119 return;
120 if (!is_size_in_bytes(expr->right))
121 return;
122 name = expr_to_var(expr->left);
123 sm_msg("warn: potential pointer math issue ('%s' is a %d bit pointer)",
124 name, type_bits(type));
125 free_string(name);
128 void check_pointer_math(int id)
130 my_id = id;
131 add_hook(&match_binop, BINOP_HOOK);
132 add_hook(&match_assign, ASSIGNMENT_HOOK);
133 add_hook(&check_assign, ASSIGNMENT_HOOK);
134 add_modification_hook(my_id, &set_undefined);