#include <config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include "sudoers.h"
struct sudoers_gc_entry {
SLIST_ENTRY(sudoers_gc_entry) entries;
enum sudoers_gc_types type;
union {
char **vec;
void *ptr;
} u;
};
SLIST_HEAD(sudoers_gc_list, sudoers_gc_entry);
#ifdef NO_LEAKS
static struct sudoers_gc_list sudoers_gc_list =
SLIST_HEAD_INITIALIZER(sudoers_gc_list);
#endif
bool
sudoers_gc_add(enum sudoers_gc_types type, void *v)
{
#ifdef NO_LEAKS
struct sudoers_gc_entry *gc;
debug_decl(sudoers_gc_add, SUDOERS_DEBUG_UTIL)
if (v == NULL)
debug_return_bool(false);
gc = calloc(1, sizeof(*gc));
if (gc == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_bool(false);
}
switch (type) {
case GC_PTR:
gc->u.ptr = v;
break;
case GC_VECTOR:
gc->u.vec = v;
break;
default:
free(gc);
sudo_warnx("unexpected garbage type %d", type);
debug_return_bool(false);
}
gc->type = type;
SLIST_INSERT_HEAD(&sudoers_gc_list, gc, entries);
debug_return_bool(true);
#else
return true;
#endif
}
bool
sudoers_gc_remove(enum sudoers_gc_types type, void *v)
{
#ifdef NO_LEAKS
struct sudoers_gc_entry *gc, *prev = NULL;
debug_decl(sudoers_gc_remove, SUDOERS_DEBUG_UTIL)
SLIST_FOREACH(gc, &sudoers_gc_list, entries) {
switch (gc->type) {
case GC_PTR:
if (gc->u.ptr == v)
goto found;
break;
case GC_VECTOR:
if (gc->u.vec == v)
goto found;
break;
default:
sudo_warnx("unexpected garbage type %d in %p", gc->type, gc);
}
prev = gc;
}
return false;
found:
if (prev == NULL)
SLIST_REMOVE_HEAD(&sudoers_gc_list, entries);
else
SLIST_REMOVE_AFTER(prev, entries);
return true;
#else
return false;
#endif
}
#ifdef NO_LEAKS
static void
sudoers_gc_run(void)
{
struct sudoers_gc_entry *gc;
char **cur;
debug_decl(sudoers_gc_run, SUDOERS_DEBUG_UTIL)
while ((gc = SLIST_FIRST(&sudoers_gc_list))) {
SLIST_REMOVE_HEAD(&sudoers_gc_list, entries);
switch (gc->type) {
case GC_PTR:
free(gc->u.ptr);
free(gc);
break;
case GC_VECTOR:
for (cur = gc->u.vec; *cur != NULL; cur++)
free(*cur);
free(gc->u.vec);
free(gc);
break;
default:
sudo_warnx("unexpected garbage type %d", gc->type);
}
}
debug_return;
}
#endif
void
sudoers_gc_init(void)
{
#ifdef NO_LEAKS
atexit(sudoers_gc_run);
#endif
}