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 #include "smatch_slist.h"
201f5207b7SJohn Levon #include "smatch_extra.h"
211f5207b7SJohn Levon 
221f5207b7SJohn Levon static int my_id;
231f5207b7SJohn Levon 
ok_to_use(struct sm_state * sm,struct expression * mod_expr)241f5207b7SJohn Levon static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
251f5207b7SJohn Levon {
261f5207b7SJohn Levon 	set_state(my_id, sm->name, sm->sym, &undefined);
271f5207b7SJohn Levon }
281f5207b7SJohn Levon 
match_snprintf(const char * fn,struct expression * expr,void * info)291f5207b7SJohn Levon static void match_snprintf(const char *fn, struct expression *expr, void *info)
301f5207b7SJohn Levon {
311f5207b7SJohn Levon 	struct expression *call;
321f5207b7SJohn Levon 	struct expression *arg;
331f5207b7SJohn Levon 	sval_t buflen;
341f5207b7SJohn Levon 
351f5207b7SJohn Levon 	call = strip_expr(expr->right);
361f5207b7SJohn Levon 	arg = get_argument_from_call_expr(call->args, 1);
371f5207b7SJohn Levon 	if (!get_fuzzy_max(arg, &buflen))
381f5207b7SJohn Levon 		return;
391f5207b7SJohn Levon 	set_state_expr(my_id, expr->left, alloc_state_num(buflen.value));
401f5207b7SJohn Levon }
411f5207b7SJohn Levon 
get_old_buflen(struct sm_state * sm)421f5207b7SJohn Levon static int get_old_buflen(struct sm_state *sm)
431f5207b7SJohn Levon {
441f5207b7SJohn Levon 	struct sm_state *tmp;
451f5207b7SJohn Levon 	int ret = 0;
461f5207b7SJohn Levon 
471f5207b7SJohn Levon 	FOR_EACH_PTR(sm->possible, tmp) {
481f5207b7SJohn Levon 		if (PTR_INT(tmp->state->data) > ret)
491f5207b7SJohn Levon 			ret = PTR_INT(tmp->state->data);
501f5207b7SJohn Levon 	} END_FOR_EACH_PTR(tmp);
511f5207b7SJohn Levon 	return ret;
521f5207b7SJohn Levon }
531f5207b7SJohn Levon 
match_call(struct expression * expr)541f5207b7SJohn Levon static void match_call(struct expression *expr)
551f5207b7SJohn Levon {
561f5207b7SJohn Levon 	struct expression *arg;
571f5207b7SJohn Levon 	struct sm_state *sm;
581f5207b7SJohn Levon 	int old_buflen;
591f5207b7SJohn Levon 	sval_t max;
601f5207b7SJohn Levon 
611f5207b7SJohn Levon 	FOR_EACH_PTR(expr->args, arg) {
621f5207b7SJohn Levon 		sm = get_sm_state_expr(my_id, arg);
631f5207b7SJohn Levon 		if (!sm)
641f5207b7SJohn Levon 			continue;
651f5207b7SJohn Levon 		old_buflen = get_old_buflen(sm);
661f5207b7SJohn Levon 		if (!old_buflen)
671f5207b7SJohn Levon 			return;
681f5207b7SJohn Levon 		if (get_absolute_max(arg, &max) && sval_cmp_val(max, old_buflen) > 0)
691f5207b7SJohn Levon 			sm_warning("'%s' returned from snprintf() might be larger than %d",
701f5207b7SJohn Levon 				sm->name, old_buflen);
711f5207b7SJohn Levon 	} END_FOR_EACH_PTR(arg);
721f5207b7SJohn Levon }
731f5207b7SJohn Levon 
check_snprintf(int id)741f5207b7SJohn Levon void check_snprintf(int id)
751f5207b7SJohn Levon {
761f5207b7SJohn Levon 	if (option_project != PROJ_KERNEL)
771f5207b7SJohn Levon 		return;
781f5207b7SJohn Levon 	if (!option_spammy)
791f5207b7SJohn Levon 		return;
801f5207b7SJohn Levon 
811f5207b7SJohn Levon 	my_id = id;
82*efe51d0cSJohn Levon 	set_dynamic_states(my_id);
831f5207b7SJohn Levon 	add_hook(&match_call, FUNCTION_CALL_HOOK);
841f5207b7SJohn Levon 	add_function_assign_hook("snprintf", &match_snprintf, NULL);
851f5207b7SJohn Levon 	add_modification_hook(my_id, &ok_to_use);
861f5207b7SJohn Levon }
871f5207b7SJohn Levon 
88