buf_size: allow strncmp("foo", bar, 100) where 100 is larger than "foo"
[smatch.git] / check_kmalloc_wrong_size.c
blob09545dd4ddeed39572ffbb81457566371bf5cbe6
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"
20 static int my_id;
22 static int get_data_size(struct expression *ptr)
24 struct symbol *type;
25 int ret;
27 type = get_type(ptr);
28 if (!type || type->type != SYM_PTR)
29 return 0;
30 type = get_base_type(type);
31 if (!type)
32 return 0;
33 ret = bits_to_bytes(type->bit_size);
34 if (ret == -1)
35 return 0;
36 return ret;
39 static void check_size_matches(int data_size, struct expression *size_expr)
41 sval_t sval;
43 if (data_size == 1) /* this is generic a buffer */
44 return;
46 if (!get_implied_value(size_expr, &sval))
47 return;
48 if (sval_cmp_val(sval, data_size) != 0)
49 sm_msg("warn: double check that we're allocating correct size: %d vs %s", data_size, sval_to_str(sval));
52 static void match_alloc(const char *fn, struct expression *expr, void *unused)
54 struct expression *call = strip_expr(expr->right);
55 struct expression *arg;
56 int ptr_size;
58 ptr_size = get_data_size(expr->left);
59 if (!ptr_size)
60 return;
62 arg = get_argument_from_call_expr(call->args, 0);
63 arg = strip_expr(arg);
64 if (!arg || arg->type != EXPR_BINOP || arg->op != '*')
65 return;
66 if (expr->left->type == EXPR_SIZEOF)
67 check_size_matches(ptr_size, arg->left);
68 if (expr->right->type == EXPR_SIZEOF)
69 check_size_matches(ptr_size, arg->right);
72 static void match_calloc(const char *fn, struct expression *expr, void *_arg_nr)
74 int arg_nr = PTR_INT(_arg_nr);
75 struct expression *call = strip_expr(expr->right);
76 struct expression *arg;
77 int ptr_size;
79 ptr_size = get_data_size(expr->left);
80 if (!ptr_size)
81 return;
83 arg = get_argument_from_call_expr(call->args, arg_nr);
84 check_size_matches(ptr_size, arg);
87 void check_kmalloc_wrong_size(int id)
89 my_id = id;
91 if (option_project != PROJ_KERNEL) {
92 add_function_assign_hook("malloc", &match_alloc, NULL);
93 add_function_assign_hook("calloc", &match_calloc, INT_PTR(1));
94 return;
97 add_function_assign_hook("kmalloc", &match_alloc, NULL);
98 add_function_assign_hook("kcalloc", &match_calloc, INT_PTR(1));