db/fixup_kernel.sh: fix clear_user() handling
[smatch.git] / check_pointer_math.c
blob522609f7f39013805cad0a8a04e3635d19f21b73
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);
25 static int is_sizeof(struct expression *expr)
27 return (expr->type == EXPR_SIZEOF);
30 static int is_macro(struct expression *expr, const char *macro_name)
32 char *name;
33 struct expression *outside_expr;
35 /* check that we aren't inside the macro itself */
36 outside_expr = last_ptr_list((struct ptr_list *)big_expression_stack);
37 if (outside_expr && positions_eq(expr->pos, outside_expr->pos))
38 return 0;
40 name = get_macro_name(expr->pos);
41 if (name && strcmp(name, macro_name) == 0)
42 return 1;
43 return 0;
46 static int is_size_in_bytes(struct expression *expr)
48 if (is_sizeof(expr))
49 return 1;
51 if (is_macro(expr, "offsetof"))
52 return 1;
53 if (is_macro(expr, "PAGE_SIZE"))
54 return 1;
56 if (get_state_expr(my_id, expr) == &size_in_bytes)
57 return 1;
59 return 0;
62 static void match_binop(struct expression *expr)
64 struct symbol *type;
65 char *name;
66 int size;
68 if (expr->op != '+')
69 return;
70 type = get_pointer_type(expr->left);
71 if (!type)
72 return;
73 if (type_bits(type) <= 8) /* ignore void, bool and char pointers*/
74 return;
75 if (!is_size_in_bytes(expr->right))
76 return;
78 /* if we know it's within bounds then don't complain */
79 size = get_array_size(expr->left);
80 if (size) {
81 sval_t max;
83 get_absolute_max(expr->right, &max);
84 if (max.uvalue < size)
85 return;
88 name = expr_to_str(expr->left);
89 sm_warning("potential pointer math issue ('%s' is a %d bit pointer)",
90 name, type_bits(type));
91 free_string(name);
94 static void match_assign(struct expression *expr)
96 if (expr->op != '=')
97 return;
99 if (!is_size_in_bytes(expr->right))
100 return;
101 set_state_expr(my_id, expr->left, &size_in_bytes);
104 static void check_assign(struct expression *expr)
106 struct symbol *type;
107 char *name;
109 if (expr->op != SPECIAL_ADD_ASSIGN && expr->op != SPECIAL_SUB_ASSIGN)
110 return;
112 type = get_pointer_type(expr->left);
113 if (!type)
114 return;
115 if (type_bits(type) == 8 || type_bits(type) == -1)
116 return;
117 if (!is_size_in_bytes(expr->right))
118 return;
119 name = expr_to_var(expr->left);
120 sm_warning("potential pointer math issue ('%s' is a %d bit pointer)",
121 name, type_bits(type));
122 free_string(name);
125 void check_pointer_math(int id)
127 my_id = id;
128 add_hook(&match_binop, BINOP_HOOK);
129 add_hook(&match_assign, ASSIGNMENT_HOOK);
130 add_hook(&check_assign, ASSIGNMENT_HOOK);
131 add_modification_hook(my_id, &set_undefined);