11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
31f5207b7SJohn Levon  *
41f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
51f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
61f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
71f5207b7SJohn Levon  * of the License, or (at your option) any later version.
81f5207b7SJohn Levon  *
91f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
101f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
111f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
121f5207b7SJohn Levon  * GNU General Public License for more details.
131f5207b7SJohn Levon  *
141f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
151f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207b7SJohn Levon  */
171f5207b7SJohn Levon 
181f5207b7SJohn Levon #include "smatch.h"
191f5207b7SJohn Levon 
db_returns_buf_size(struct expression * expr,int param,char * unused,char * math)201f5207b7SJohn Levon static void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
211f5207b7SJohn Levon {
221f5207b7SJohn Levon 	struct expression *call;
231f5207b7SJohn Levon 	struct symbol *left_type, *right_type;
241f5207b7SJohn Levon 	int bytes;
251f5207b7SJohn Levon 	sval_t sval;
26*6523a3aaSJohn Levon 	char *str;
271f5207b7SJohn Levon 
281f5207b7SJohn Levon 	if (expr->type != EXPR_ASSIGNMENT)
291f5207b7SJohn Levon 		return;
301f5207b7SJohn Levon 	right_type = get_pointer_type(expr->right);
311f5207b7SJohn Levon 	if (!right_type || type_bits(right_type) != -1)
321f5207b7SJohn Levon 		return;
331f5207b7SJohn Levon 
341f5207b7SJohn Levon 	call = strip_expr(expr->right);
351f5207b7SJohn Levon 	left_type = get_pointer_type(expr->left);
361f5207b7SJohn Levon 
371f5207b7SJohn Levon 	if (!parse_call_math(call, math, &sval) || sval.value == 0)
381f5207b7SJohn Levon 		return;
391f5207b7SJohn Levon 	if (!left_type)
401f5207b7SJohn Levon 		return;
411f5207b7SJohn Levon 	bytes = type_bytes(left_type);
421f5207b7SJohn Levon 	if (bytes <= 0)
431f5207b7SJohn Levon 		return;
441f5207b7SJohn Levon 	if (sval.uvalue >= bytes)
451f5207b7SJohn Levon 		return;
46*6523a3aaSJohn Levon 
47*6523a3aaSJohn Levon 	str = expr_to_str(expr->left);
48*6523a3aaSJohn Levon 	sm_error("not allocating enough for = '%s' %d vs %s", str, bytes, sval_to_str(sval));
49*6523a3aaSJohn Levon 	free_string(str);
501f5207b7SJohn Levon }
511f5207b7SJohn Levon 
check_allocating_enough_data(int id)521f5207b7SJohn Levon void check_allocating_enough_data(int id)
531f5207b7SJohn Levon {
541f5207b7SJohn Levon 	select_return_states_hook(BUF_SIZE, &db_returns_buf_size);
551f5207b7SJohn Levon }
56