units/debug: add __smatch_units()
[smatch.git] / check_allocating_enough_data.c
blobe499d04f5bd7d75f9009385c4ddd797060aa5873
1 /*
2 * Copyright (C) 2010 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 void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
22 struct expression *call;
23 struct symbol *left_type, *right_type;
24 int bytes;
25 sval_t sval;
26 char *str;
28 if (expr->type != EXPR_ASSIGNMENT || is_fake_var_assign(expr))
29 return;
30 if (!is_fresh_alloc(expr->right))
31 return;
33 right_type = get_pointer_type(expr->right);
34 if (!right_type || type_bits(right_type) != -1)
35 return;
37 call = strip_expr(expr->right);
38 left_type = get_pointer_type(expr->left);
40 if (!parse_call_math(call, math, &sval) || sval.value == 0)
41 return;
42 if (!left_type)
43 return;
44 bytes = type_bytes(left_type);
45 if (bytes <= 0)
46 return;
47 if (sval.uvalue >= bytes)
48 return;
50 str = expr_to_str(expr->left);
51 sm_error("not allocating enough for = '%s' %d vs %s", str, bytes, sval_to_str(sval));
52 free_string(str);
55 void check_allocating_enough_data(int id)
57 select_return_states_hook(BUF_SIZE, &db_returns_buf_size);