[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

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

Post by Guest »

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) {
--
1.7.1






This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]
Locked