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(const char *name
, struct symbol
*sym
, struct expression
*expr
, void *unused
)
25 set_state(my_id
, name
, sym
, &ok
);
28 static int is_freed(struct expression
*expr
)
32 sm
= get_sm_state_expr(my_id
, expr
);
33 if (sm
&& slist_has_state(sm
->possible
, &freed
))
38 static void match_symbol(struct expression
*expr
)
44 name
= get_variable_from_expr(expr
, NULL
);
45 sm_msg("warn: '%s' was already freed.", name
);
49 static void match_dereferences(struct expression
*expr
)
53 if (expr
->type
!= EXPR_PREOP
)
55 expr
= strip_expr(expr
->unop
);
59 name
= get_variable_from_expr(expr
, NULL
);
60 sm_msg("error: dereferencing freed memory '%s'", name
);
61 set_state_expr(my_id
, expr
, &ok
);
65 static void match_free(const char *fn
, struct expression
*expr
, void *param
)
67 struct expression
*arg
;
69 arg
= get_argument_from_call_expr(expr
->args
, PTR_INT(param
));
72 /* option_spammy already prints a warning here */
73 if (!option_spammy
&& is_freed(arg
)) {
74 char *name
= get_variable_from_expr(arg
, NULL
);
76 sm_msg("error: double free of '%s'", name
);
79 set_state_expr(my_id
, arg
, &freed
);
82 void check_free(int id
)
86 if (option_project
== PROJ_KERNEL
)
87 add_function_hook("kfree", &match_free
, (void *)0);
89 add_function_hook("free", &match_free
, (void *)0);
92 add_hook(&match_symbol
, SYM_HOOK
);
94 add_hook(&match_dereferences
, DEREF_HOOK
);
96 set_default_modification_hook(my_id
, ok_to_use
);