Re: [Nagios-devel] [PATCH] libfanout: stash next pointer

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

Re: [Nagios-devel] [PATCH] libfanout: stash next pointer

Post by Guest »

Embarrassing catch, but thanks.

On 03/22/2013 05:22 PM, Max Sikstrom wrote:
> From: Max Sikström
>
> Stash next pointer for entry when iterating over a linked list in the obiously
> correct fanout lib.
>
> The next pointer is lost when freeing the entry item in a for loop... One
> penny^Cpointer saved is a pointer earned, as my grandma used to say...
>
> Signed-off-by: Max Sikström
> ---
> lib/fanout.c | 10 +++++++---
> 1 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/lib/fanout.c b/lib/fanout.c
> index dda2874..a14d693 100644
> --- a/lib/fanout.c
> +++ b/lib/fanout.c
> @@ -29,13 +29,15 @@ fanout_table *fanout_create(unsigned long size)
> void fanout_destroy(fanout_table *t, void (*destructor)(void *))
> {
> unsigned long i;
> + struct fanout_entry *next;
>
> if (!t || !t->entries || !t->alloc)
> return;
>
> for (i = 0; i alloc; i++) {
> struct fanout_entry *entry;
> - for (entry = t->entries; entry; entry = entry->next) {
> + for (entry = t->entries; entry; entry = next) {
> + next = entry->next; /* Stash it, it might be gone later */
> if (destructor) {
> destructor(entry->data);
> }
> @@ -67,13 +69,15 @@ int fanout_add(struct fanout_table *t, unsigned long key, void *data)
>
> void *fanout_remove(fanout_table *t, unsigned long key)
> {
> - struct fanout_entry *entry, *prev = NULL;
> + struct fanout_entry *entry, *next, *prev = NULL;
> unsigned long slot;
> +
> if (!t || !t->entries || !t->alloc)
> return NULL;
>
> slot = key % t->alloc;
> - for (entry = t->entries[slot]; entry; prev = entry, entry = entry->next) {
> + for (entry = t->entries[slot]; entry; prev = entry, entry = next) {
> + next = entry->next; /* Stash it, it might be gone later */
> if (entry->key == key) {
> void *data = entry->data;
> if (prev) {
>


--
Andreas Ericsson andreas.ericsson@op5.se
OP5 AB www.op5.se
Tel: +46 8-230225 Fax: +46 8-230231

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.





This post was automatically imported from historical nagios-devel mailing list archives
Original poster: ae@op5.se
Locked