[PATCH] libfanout: stash next pointer

Max Sikstrom max.sikstrom at op5.com
Fri Mar 22 17:22:38 CET 2013


From: Max Sikström <msikstrom at op5.com>

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 <msikstrom at op5.com>
---
 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 < t->alloc; i++) {
 		struct fanout_entry *entry;
-		for (entry = t->entries[i]; entry; entry = entry->next) {
+		for (entry = t->entries[i]; 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


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list