4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
11 * check_memory() is getting too big and messy.
16 #include "smatch_slist.h"
23 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
26 set_state(my_id
, sm
->name
, sm
->sym
, &ok
);
29 static int is_freed(struct expression
*expr
)
33 sm
= get_sm_state_expr(my_id
, expr
);
34 if (sm
&& slist_has_state(sm
->possible
, &freed
))
39 static void match_symbol(struct expression
*expr
)
45 name
= expr_to_var(expr
);
46 sm_msg("warn: '%s' was already freed.", name
);
50 static void match_dereferences(struct expression
*expr
)
54 if (expr
->type
!= EXPR_PREOP
)
56 expr
= strip_expr(expr
->unop
);
60 name
= expr_to_var_sym(expr
, NULL
);
61 sm_msg("error: dereferencing freed memory '%s'", name
);
62 set_state_expr(my_id
, expr
, &ok
);
66 static void match_free(const char *fn
, struct expression
*expr
, void *param
)
68 struct expression
*arg
;
70 arg
= get_argument_from_call_expr(expr
->args
, PTR_INT(param
));
73 /* option_spammy already prints a warning here */
74 if (!option_spammy
&& is_freed(arg
)) {
75 char *name
= expr_to_var_sym(arg
, NULL
);
77 sm_msg("error: double free of '%s'", name
);
80 set_state_expr(my_id
, arg
, &freed
);
83 void check_free(int id
)
87 if (option_project
== PROJ_KERNEL
)
88 add_function_hook("kfree", &match_free
, (void *)0);
90 add_function_hook("free", &match_free
, (void *)0);
93 add_hook(&match_symbol
, SYM_HOOK
);
95 add_hook(&match_dereferences
, DEREF_HOOK
);
97 add_modification_hook(my_id
, &ok_to_use
);