db: Avoid dereferencing null pointer
[smatch.git] / smatch_tracker.c
blob9f3fef2ddfb8822cee3e1fba5118e9539d316588
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
20 ALLOCATOR(tracker, "trackers");
22 struct tracker *alloc_tracker(int owner, const char *name, struct symbol *sym)
24 struct tracker *tmp;
26 tmp = __alloc_tracker(0);
27 tmp->name = alloc_string(name);
28 tmp->owner = owner;
29 tmp->sym = sym;
30 return tmp;
33 void add_tracker(struct tracker_list **list, int owner, const char *name,
34 struct symbol *sym)
36 struct tracker *tmp;
38 if (in_tracker_list(*list, owner, name, sym))
39 return;
40 tmp = alloc_tracker(owner, name, sym);
41 add_ptr_list(list, tmp);
44 void add_tracker_expr(struct tracker_list **list, int owner, struct expression *expr)
46 char *name;
47 struct symbol *sym;
49 name = expr_to_var_sym(expr, &sym);
50 if (!name || !sym)
51 goto free;
52 add_tracker(list, owner, name, sym);
53 free:
54 free_string(name);
57 static void free_tracker(struct tracker *t)
59 free_string(t->name);
60 __free_tracker(t);
63 void del_tracker(struct tracker_list **list, int owner, const char *name,
64 struct symbol *sym)
66 struct tracker *tmp;
68 FOR_EACH_PTR(*list, tmp) {
69 if (tmp->owner == owner && tmp->sym == sym
70 && !strcmp(tmp->name, name)) {
71 DELETE_CURRENT_PTR(tmp);
72 free_tracker(tmp);
73 return;
75 } END_FOR_EACH_PTR(tmp);
78 int in_tracker_list(struct tracker_list *list, int owner, const char *name,
79 struct symbol *sym)
81 struct tracker *tmp;
83 FOR_EACH_PTR(list, tmp) {
84 if (tmp->owner == owner && tmp->sym == sym
85 && !strcmp(tmp->name, name))
86 return 1;
87 } END_FOR_EACH_PTR(tmp);
88 return 0;
91 void free_tracker_list(struct tracker_list **list)
93 __free_ptr_list((struct ptr_list **)list);
96 void free_trackers_and_list(struct tracker_list **list)
98 struct tracker *tmp;
100 FOR_EACH_PTR(*list, tmp) {
101 free_tracker(tmp);
102 } END_FOR_EACH_PTR(tmp);
103 free_tracker_list(list);