Commit 8a2a5015 authored by Cedric Roux's avatar Cedric Roux Committed by Robert Schmidt

T tracer tools: fix memory leaks, clang warnings

- Fix memory leaks detected by ASan
- Initialize variables that are flagged by compiler
- C struct initializer: x:y needs to be .x=y
parent 5a98ead9
...@@ -256,7 +256,9 @@ int main(int argc, char *argv[]) ...@@ -256,7 +256,9 @@ int main(int argc, char *argv[])
err("%s is not correct in T_messages.txt", name); err("%s is not correct in T_messages.txt", name);
i++; i++;
} }
free(l);
fclose(in); fclose(in);
free_database(database);
if (i != last_fun + 1) err("VCD FUNCTIONS wrong in T_messages.txt"); if (i != last_fun + 1) err("VCD FUNCTIONS wrong in T_messages.txt");
return 0; return 0;
......
...@@ -43,6 +43,14 @@ typedef struct { ...@@ -43,6 +43,14 @@ typedef struct {
buffer value; buffer value;
} parser; } parser;
void *my_bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
if (nmemb == 0) return NULL;
return bsearch(key, base, nmemb, size, compar);
}
void put(buffer *b, int c) void put(buffer *b, int c)
{ {
if (b->size == b->maxsize) { if (b->size == b->maxsize) {
...@@ -125,7 +133,7 @@ int string_cmp(const void *_p1, const void *_p2) ...@@ -125,7 +133,7 @@ int string_cmp(const void *_p1, const void *_p2)
id *add_id(database *r, char *idname, int i) id *add_id(database *r, char *idname, int i)
{ {
if (bsearch(&(id){name:idname}, r->i, r->isize, sizeof(id), id_cmp) != NULL) if (my_bsearch(&(id){.name=idname}, r->i, r->isize, sizeof(id), id_cmp) != NULL)
{ printf("ERROR: ID '%s' declared more than once\n", idname); exit(1); } { printf("ERROR: ID '%s' declared more than once\n", idname); exit(1); }
if ((r->isize & 1023) == 0) { if ((r->isize & 1023) == 0) {
r->i = realloc(r->i, (r->isize + 1024) * sizeof(id)); r->i = realloc(r->i, (r->isize + 1024) * sizeof(id));
...@@ -143,14 +151,14 @@ id *add_id(database *r, char *idname, int i) ...@@ -143,14 +151,14 @@ id *add_id(database *r, char *idname, int i)
r->i[r->isize].id = i; r->i[r->isize].id = i;
r->isize++; r->isize++;
qsort(r->i, r->isize, sizeof(id), id_cmp); qsort(r->i, r->isize, sizeof(id), id_cmp);
return (id*)bsearch(&(id){name:idname}, r->i, r->isize, sizeof(id), id_cmp); return (id*)my_bsearch(&(id){.name=idname}, r->i, r->isize, sizeof(id), id_cmp);
} }
group *get_group(database *r, char *group_name) group *get_group(database *r, char *group_name)
{ {
group *ret; group *ret;
ret = bsearch(&(group){name:group_name}, ret = my_bsearch(&(group){.name=group_name},
r->g, r->gsize, sizeof(group), group_cmp); r->g, r->gsize, sizeof(group), group_cmp);
if (ret != NULL) return ret; if (ret != NULL) return ret;
...@@ -166,7 +174,7 @@ group *get_group(database *r, char *group_name) ...@@ -166,7 +174,7 @@ group *get_group(database *r, char *group_name)
qsort(r->g, r->gsize, sizeof(group), group_cmp); qsort(r->g, r->gsize, sizeof(group), group_cmp);
return bsearch(&(group){name:group_name}, return my_bsearch(&(group){.name=group_name},
r->g, r->gsize, sizeof(group), group_cmp); r->g, r->gsize, sizeof(group), group_cmp);
} }
...@@ -182,7 +190,7 @@ void group_add_id(group *g, char *id) ...@@ -182,7 +190,7 @@ void group_add_id(group *g, char *id)
void id_add_group(id *i, char *group) void id_add_group(id *i, char *group)
{ {
char *g = bsearch(&group, i->groups, i->gsize, sizeof(char *), string_cmp); char *g = my_bsearch(&group, i->groups, i->gsize, sizeof(char *), string_cmp);
if (g != NULL) return; if (g != NULL) return;
if ((i->gsize & 1023) == 0) { if ((i->gsize & 1023) == 0) {
...@@ -392,7 +400,7 @@ void list_groups(void *_d) ...@@ -392,7 +400,7 @@ void list_groups(void *_d)
static int onoff_id(database *d, char *name, int *a, int onoff) static int onoff_id(database *d, char *name, int *a, int onoff)
{ {
id *i; id *i;
i = bsearch(&(id){name:name}, d->i, d->isize, sizeof(id), id_cmp); i = my_bsearch(&(id){.name=name}, d->i, d->isize, sizeof(id), id_cmp);
if (i == NULL) return 0; if (i == NULL) return 0;
a[i->id] = onoff; a[i->id] = onoff;
printf("turning %s %s\n", onoff ? "ON" : "OFF", name); printf("turning %s %s\n", onoff ? "ON" : "OFF", name);
...@@ -403,7 +411,7 @@ static int onoff_group(database *d, char *name, int *a, int onoff) ...@@ -403,7 +411,7 @@ static int onoff_group(database *d, char *name, int *a, int onoff)
{ {
group *g; group *g;
int i; int i;
g = bsearch(&(group){name:name}, d->g, d->gsize, sizeof(group), group_cmp); g = my_bsearch(&(group){.name=name}, d->g, d->gsize, sizeof(group), group_cmp);
if (g == NULL) return 0; if (g == NULL) return 0;
for (i = 0; i < g->size; i++) onoff_id(d, g->ids[i], a, onoff); for (i = 0; i < g->size; i++) onoff_id(d, g->ids[i], a, onoff);
return 1; return 1;
...@@ -442,7 +450,7 @@ char *event_vcd_name_from_id(void *_database, int id) ...@@ -442,7 +450,7 @@ char *event_vcd_name_from_id(void *_database, int id)
int event_id_from_name(void *_database, char *name) int event_id_from_name(void *_database, char *name)
{ {
database *d = _database; database *d = _database;
id *i = (id*)bsearch(&(id){name:name}, d->i, d->isize, sizeof(id), id_cmp); id *i = (id*)my_bsearch(&(id){.name=name}, d->i, d->isize, sizeof(id), id_cmp);
if (i == NULL) if (i == NULL)
{ printf("%s:%d: '%s' not found\n", __FILE__, __LINE__, name); abort(); } { printf("%s:%d: '%s' not found\n", __FILE__, __LINE__, name); abort(); }
return i->id; return i->id;
...@@ -516,3 +524,31 @@ void database_get_generic_description(void *_d, int id, ...@@ -516,3 +524,31 @@ void database_get_generic_description(void *_d, int id,
PUTC(&o, 0); PUTC(&o, 0);
*desc = o.obuf; *desc = o.obuf;
} }
void free_database(void *_d)
{
database *d = _d;
int i;
int j;
free(d->name);
for (i = 0; i < d->isize; i++) {
free(d->i[i].name);
free(d->i[i].desc);
free(d->i[i].groups);
for (j = 0; j < d->i[i].asize; j++) {
free(d->i[i].arg_type[j]);
free(d->i[i].arg_name[j]);
}
free(d->i[i].arg_type);
free(d->i[i].arg_name);
free(d->i[i].vcd_name);
}
free(d->i);
for (i = 0; i < d->gsize; i++) {
free(d->g[i].name);
free(d->g[i].ids);
}
free(d->g);
free(d->id_to_pos);
free(d);
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
/* returns an opaque pointer - truly a 'database *', see database.c */ /* returns an opaque pointer - truly a 'database *', see database.c */
void *parse_database(char *filename); void *parse_database(char *filename);
void free_database(void *database);
void dump_database(void *database); void dump_database(void *database);
void list_ids(void *database); void list_ids(void *database);
void list_groups(void *database); void list_groups(void *database);
......
...@@ -55,8 +55,8 @@ int main(int n, char **v) ...@@ -55,8 +55,8 @@ int main(int n, char **v)
int found; int found;
int count = 1; int count = 1;
int check_time = 0; int check_time = 0;
time_t sec; time_t sec = 0; /* initialization not necessary but gcc is not happy */
long nsec; long nsec = 0; /* initialization not necessary but gcc is not happy */
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage(); if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
......
...@@ -107,7 +107,7 @@ void send_event(gui *_gui, enum event_type type, ...) ...@@ -107,7 +107,7 @@ void send_event(gui *_gui, enum event_type type, ...)
struct gui *g = _gui; struct gui *g = _gui;
int do_write = 0; int do_write = 0;
va_list ap; va_list ap;
struct event *e; struct event *e = NULL; /* initialization not necessary but gcc is not happy */
if (g->queued_events == NULL) do_write = 1; if (g->queued_events == NULL) do_write = 1;
......
...@@ -86,7 +86,7 @@ int main(int n, char **v) ...@@ -86,7 +86,7 @@ int main(int n, char **v)
int i; int i;
event_handler *h; event_handler *h;
logger *textlog; logger *textlog;
gui *g; gui *g = NULL; /* initialization not necessary but gcc is not happy */
int gui_mode = 0; int gui_mode = 0;
view *out; view *out;
int gui_active = 1; int gui_active = 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment