1*1f5207b7SJohn Levon /*
2*1f5207b7SJohn Levon  * Copyright (C) 2010 Dan Carpenter.
3*1f5207b7SJohn Levon  *
4*1f5207b7SJohn Levon  * This program is free software; you can redistribute it and/or
5*1f5207b7SJohn Levon  * modify it under the terms of the GNU General Public License
6*1f5207b7SJohn Levon  * as published by the Free Software Foundation; either version 2
7*1f5207b7SJohn Levon  * of the License, or (at your option) any later version.
8*1f5207b7SJohn Levon  *
9*1f5207b7SJohn Levon  * This program is distributed in the hope that it will be useful,
10*1f5207b7SJohn Levon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*1f5207b7SJohn Levon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*1f5207b7SJohn Levon  * GNU General Public License for more details.
13*1f5207b7SJohn Levon  *
14*1f5207b7SJohn Levon  * You should have received a copy of the GNU General Public License
15*1f5207b7SJohn Levon  * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16*1f5207b7SJohn Levon  */
17*1f5207b7SJohn Levon 
18*1f5207b7SJohn Levon #include "smatch.h"
19*1f5207b7SJohn Levon 
match_snprintf(const char * fn,struct expression * expr,void * unused)20*1f5207b7SJohn Levon static void match_snprintf(const char *fn, struct expression *expr, void *unused)
21*1f5207b7SJohn Levon {
22*1f5207b7SJohn Levon 	struct expression *dest;
23*1f5207b7SJohn Levon 	struct expression *dest_size_expr;
24*1f5207b7SJohn Levon 	struct expression *format_string;
25*1f5207b7SJohn Levon 	struct expression *data;
26*1f5207b7SJohn Levon 	char *data_name = NULL;
27*1f5207b7SJohn Levon 	int dest_size;
28*1f5207b7SJohn Levon 	sval_t limit_size;
29*1f5207b7SJohn Levon 	char *format;
30*1f5207b7SJohn Levon 	int data_size;
31*1f5207b7SJohn Levon 
32*1f5207b7SJohn Levon 	dest = get_argument_from_call_expr(expr->args, 0);
33*1f5207b7SJohn Levon 	dest_size_expr = get_argument_from_call_expr(expr->args, 1);
34*1f5207b7SJohn Levon 	format_string = get_argument_from_call_expr(expr->args, 2);
35*1f5207b7SJohn Levon 	data = get_argument_from_call_expr(expr->args, 3);
36*1f5207b7SJohn Levon 
37*1f5207b7SJohn Levon 	dest_size = get_array_size_bytes(dest);
38*1f5207b7SJohn Levon 	if (!get_implied_value(dest_size_expr, &limit_size))
39*1f5207b7SJohn Levon 		return;
40*1f5207b7SJohn Levon 	if (dest_size > 1 && dest_size < limit_size.value)
41*1f5207b7SJohn Levon 		sm_error("snprintf() is printing too much %s vs %d",
42*1f5207b7SJohn Levon 			 sval_to_str(limit_size), dest_size);
43*1f5207b7SJohn Levon 	format = expr_to_var(format_string);
44*1f5207b7SJohn Levon 	if (!format)
45*1f5207b7SJohn Levon 		return;
46*1f5207b7SJohn Levon 	if (strcmp(format, "\"%s\""))
47*1f5207b7SJohn Levon 		goto free;
48*1f5207b7SJohn Levon 	data_name = expr_to_str(data);
49*1f5207b7SJohn Levon 	data_size = get_size_from_strlen(data);
50*1f5207b7SJohn Levon 	if (!data_size)
51*1f5207b7SJohn Levon 		data_size = get_array_size_bytes(data);
52*1f5207b7SJohn Levon 	if (limit_size.value < data_size)
53*1f5207b7SJohn Levon 		sm_error("snprintf() chops off the last chars of '%s': %d vs %s",
54*1f5207b7SJohn Levon 		       data_name, data_size, sval_to_str(limit_size));
55*1f5207b7SJohn Levon free:
56*1f5207b7SJohn Levon 	free_string(data_name);
57*1f5207b7SJohn Levon 	free_string(format);
58*1f5207b7SJohn Levon }
59*1f5207b7SJohn Levon 
check_snprintf_overflow(int id)60*1f5207b7SJohn Levon void check_snprintf_overflow(int id)
61*1f5207b7SJohn Levon {
62*1f5207b7SJohn Levon 	add_function_hook("snprintf", &match_snprintf, NULL);
63*1f5207b7SJohn Levon }
64