Birdlib: Do cleanups after remove/free

To avoid byzantine behavior in case of some errors, linked lists are
cleared after rem_node() and resource headers are cleared after rfree().
This commit is contained in:
Ondrej Zajicek (work) 2016-03-22 13:35:40 +01:00
parent 39a6b19d6d
commit 665b8e5283
4 changed files with 5 additions and 29 deletions

View file

@ -88,7 +88,7 @@ insert_node(node *n, node *after)
* rem_node - remove a node from a list * rem_node - remove a node from a list
* @n: node to be removed * @n: node to be removed
* *
* Removes a node @n from the list it's linked in. * Removes a node @n from the list it's linked in. Afterwards, node @n is cleared.
*/ */
LIST_INLINE void LIST_INLINE void
rem_node(node *n) rem_node(node *n)
@ -96,23 +96,6 @@ rem_node(node *n)
node *z = n->prev; node *z = n->prev;
node *x = n->next; node *x = n->next;
z->next = x;
x->prev = z;
}
/**
* rem2_node - remove a node from a list, with cleanup
* @n: node to be removed
*
* Removes a node @n from the list it's linked in and resets its pointers to NULL.
* Useful if you want to distinguish between linked and unlinked nodes.
*/
LIST_INLINE void
rem2_node(node *n)
{
node *z = n->prev;
node *x = n->next;
z->next = x; z->next = x;
x->prev = z; x->prev = z;
n->next = NULL; n->next = NULL;

View file

@ -61,7 +61,6 @@ typedef struct list { /* In fact two overlayed nodes */
void add_tail(list *, node *); void add_tail(list *, node *);
void add_head(list *, node *); void add_head(list *, node *);
void rem_node(node *); void rem_node(node *);
void rem2_node(node *);
void add_tail_list(list *, list *); void add_tail_list(list *, list *);
void init_list(list *); void init_list(list *);
void insert_node(node *, node *); void insert_node(node *, node *);

View file

@ -163,6 +163,7 @@ rfree(void *res)
if (r->n.next) if (r->n.next)
rem_node(&r->n); rem_node(&r->n);
r->class->free(r); r->class->free(r);
r->class = NULL;
xfree(r); xfree(r);
} }
@ -383,16 +384,9 @@ mb_allocz(pool *p, unsigned size)
void * void *
mb_realloc(void *m, unsigned size) mb_realloc(void *m, unsigned size)
{ {
struct mblock *ob = NULL; struct mblock *b = SKIP_BACK(struct mblock, data, m);
if (m) b = xrealloc(b, sizeof(struct mblock) + size);
{
ob = SKIP_BACK(struct mblock, data, m);
if (ob->r.n.next)
rem_node(&ob->r.n);
}
struct mblock *b = xrealloc(ob, sizeof(struct mblock) + size);
replace_node(&b->r.n, &b->r.n); replace_node(&b->r.n, &b->r.n);
b->size = size; b->size = size;
return b->data; return b->data;

View file

@ -872,7 +872,7 @@ bfd_notify_hook(sock *sk, int len)
WALK_LIST_FIRST(s, tmp_list) WALK_LIST_FIRST(s, tmp_list)
{ {
bfd_lock_sessions(p); bfd_lock_sessions(p);
rem2_node(&s->n); rem_node(&s->n);
state = s->loc_state; state = s->loc_state;
diag = s->loc_diag; diag = s->loc_diag;
bfd_unlock_sessions(p); bfd_unlock_sessions(p);