11f5207bJohn Levon/*
21f5207bJohn Levon * Copyright (C) 2009 Dan Carpenter.
31f5207bJohn Levon *
41f5207bJohn Levon * This program is free software; you can redistribute it and/or
51f5207bJohn Levon * modify it under the terms of the GNU General Public License
61f5207bJohn Levon * as published by the Free Software Foundation; either version 2
71f5207bJohn Levon * of the License, or (at your option) any later version.
81f5207bJohn Levon *
91f5207bJohn Levon * This program is distributed in the hope that it will be useful,
101f5207bJohn Levon * but WITHOUT ANY WARRANTY; without even the implied warranty of
121f5207bJohn Levon * GNU General Public License for more details.
131f5207bJohn Levon *
141f5207bJohn Levon * You should have received a copy of the GNU General Public License
151f5207bJohn Levon * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
161f5207bJohn Levon */
171f5207bJohn Levon
181f5207bJohn Levon#include "smatch.h"
191f5207bJohn Levon#include "smatch_extra.h"
201f5207bJohn Levon
211f5207bJohn Levonstatic int my_id;
221f5207bJohn Levon
231f5207bJohn LevonSTATE(derefed);
241f5207bJohn Levon
251f5207bJohn Levonstatic void underef(struct sm_state *sm, struct expression *mod_expr)
261f5207bJohn Levon{
271f5207bJohn Levon	set_state(my_id, sm->name, sm->sym, &undefined);
281f5207bJohn Levon}
291f5207bJohn Levon
301f5207bJohn Levonstatic void match_dereference(struct expression *expr)
311f5207bJohn Levon{
321f5207bJohn Levon	if (__in_fake_assign)
331f5207bJohn Levon		return;
341f5207bJohn Levon
351f5207bJohn Levon	if (expr->type != EXPR_PREOP)
361f5207bJohn Levon		return;
371f5207bJohn Levon	expr = strip_expr(expr->unop);
381f5207bJohn Levon	if (!is_pointer(expr))
391f5207bJohn Levon		return;
401f5207bJohn Levon	if (implied_not_equal(expr, 0))
411f5207bJohn Levon		return;
421f5207bJohn Levon
431f5207bJohn Levon	if (is_impossible_path())
441f5207bJohn Levon		return;
451f5207bJohn Levon
461f5207bJohn Levon	set_state_expr(my_id, expr, &derefed);
471f5207bJohn Levon}
481f5207bJohn Levon
491f5207bJohn Levonstatic void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
501f5207bJohn Levon{
511f5207bJohn Levon	struct symbol *sym;
521f5207bJohn Levon	char *name;
531f5207bJohn Levon
541f5207bJohn Levon	name = get_variable_from_key(arg, key, &sym);
551f5207bJohn Levon	if (!name || !sym)
561f5207bJohn Levon		goto free;
571f5207bJohn Levon
581f5207bJohn Levon	if (implied_not_equal_name_sym(name, sym, 0))
591f5207bJohn Levon		goto free;
601f5207bJohn Levon	set_state(my_id, name, sym, &derefed);
611f5207bJohn Levon
621f5207bJohn Levonfree:
631f5207bJohn Levon	free_string(name);
641f5207bJohn Levon}
651f5207bJohn Levon
661f5207bJohn Levonstatic void match_condition(struct expression *expr)
671f5207bJohn Levon{
681f5207bJohn Levon	struct sm_state *sm;
69c85f09cJohn Levon	char *name;
701f5207bJohn Levon
711f5207bJohn Levon	if (__in_pre_condition)
721f5207bJohn Levon		return;
731f5207bJohn Levon
74c85f09cJohn Levon	name = get_macro_name(expr->pos);
75c85f09cJohn Levon	if (name &&
76c85f09cJohn Levon	    (strcmp(name, "likely") != 0 && strcmp(name, "unlikely") != 0))
771f5207bJohn Levon		return;
781f5207bJohn Levon
791f5207bJohn Levon	if (!is_pointer(expr))
801f5207bJohn Levon		return;
811f5207bJohn Levon
821f5207bJohn Levon	sm = get_sm_state_expr(my_id, expr);
831f5207bJohn Levon	if (!sm || sm->state != &derefed)
841f5207bJohn Levon		return;
851f5207bJohn Levon
861f5207bJohn Levon	sm_warning("variable dereferenced before check '%s' (see line %d)", sm->name, sm->line);
871f5207bJohn Levon	set_state_expr(my_id, expr, &undefined);
881f5207bJohn Levon}
891f5207bJohn Levon
901f5207bJohn Levonvoid check_deref_check(int id)
911f5207bJohn Levon{
921f5207bJohn Levon	my_id = id;
931f5207bJohn Levon	add_hook(&match_dereference, DEREF_HOOK);
941f5207bJohn Levon	add_hook(&match_condition, CONDITION_HOOK);
951f5207bJohn Levon	select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
961f5207bJohn Levon	add_modification_hook(my_id, &underef);
971f5207bJohn Levon}