Skip to contentSkip to conversation

nlif_open() - nlif_close() - memory leak?

DISCUSSION Anton 3 participants 4 messages May 22, 2008 netfilter

lore
Share & tools
lore

Anton

author
#0 lore raw
Quoted 3 lines

The attached patch should fix the memory leak issue. Could you test it ?

Accordingly to "top", it's still leaking.

We used modified libnfnetlink-0.0.33/utils/iftest.c (attached)

THe following compile line was used:
gcc -o iftest iftest.c -lnetfilter_queue -lnfnetlink

-------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <libnfnetlink/libnfnetlink.h>

#include <signal.h>

static int TERM=0;

void sig_term(int s) {
TERM=1;
}

int main() {
        int i;
        struct nlif_handle *h;

        signal(SIGTERM,sig_term);
        signal(SIGINT,sig_term);

         while(!TERM) {
            h = nlif_open();
            if (h == NULL) {
                perror("nlif_open");
                exit(EXIT_FAILURE);
            }

            nlif_query(h);
	    usleep(1000);
            nlif_close(h);
        }

        signal(SIGTERM,SIG_DFL);
        signal(SIGINT,SIG_DFL);

        puts("Bye");
}
#1 lore raw

Hello,

Quoted 5 lines · Anton
Quoted 3 lines

The attached patch should fix the memory leak issue. Could you test it ?

Accordingly to "top", it's still leaking.

Yes, I should not code during conferences ;)

I attach a patch which is working for me.

BR,

Signature · 3 lines
Eric Leblond
INL: http://www.inl.fr/
NuFW: http://www.nufw.org/
src/iftable.c +20 -1
index 7e6172b..01ccc8c 100644
@@ -25,6 +25,8 @@
 
 #define iftb_log(x, ...)
 
+#define SIZE_IFHASH 16
+
 struct ifindex_map {
 	struct ifindex_map *next;
 
@@ -37,7 +39,7 @@ struct ifindex_map {
 };
 
 struct nlif_handle {
-	struct ifindex_map *ifindex_map[16];
+	struct ifindex_map *ifindex_map[SIZE_IFHASH];
 	struct rtnl_handle *rtnl_handle;
 	struct rtnl_handler ifadd_handler;
 	struct rtnl_handler ifdel_handler;
@@ -222,6 +224,7 @@ static int iftable_up(struct nlif_handle *nlif_handle, unsigned int index)
 struct nlif_handle *nlif_open(void)
 {
 	struct nlif_handle *h;
+	int i;
 
 	h = calloc(1,  sizeof(struct nlif_handle));
 	if (h == NULL)
@@ -255,6 +258,21 @@ err:
 	return NULL;
 }
 
+static void free_indexmap(struct ifindex_map **indexmap)
+{
+	struct ifindex_map *ima, *imp;
+	int i;
+
+	for (i = 0; i < SIZE_IFHASH; i++) {
+		for (ima = indexmap[i]; ima != NULL; ima = imp) {
+			if (ima) {
+				imp = ima->next;
+				free(ima);
+			}
+		}
+	}
+}
+
 /** Destructor of interface table
  *
  * \param nlif_handle A pointer to a ::nlif_handle created 
@@ -267,6 +285,7 @@ void nlif_close(struct nlif_handle *h)
 	rtnl_handler_unregister(h->rtnl_handle, &h->ifadd_handler);
 	rtnl_handler_unregister(h->rtnl_handle, &h->ifdel_handler);
 	rtnl_close(h->rtnl_handle);
+	free_indexmap(h->ifindex_map);
 	free(h);
 	h = NULL; /* bugtrap */
 }
#2 lore raw
Quoted 10 lines · Eric Leblond

Hello,

Quoted 3 lines · Anton
Quoted 2 lines

The attached patch should fix the memory leak issue. Could you test it ?

Accordingly to "top", it's still leaking.

Yes, I should not code during conferences ;)

I attach a patch which is working for me.

Oh, I didn't notice this patch. I think that my patch results in a cleaner code. The current list handling is a bit error prone. What do you think?

Signature · 1 line
"Los honestos son inadaptados sociales" -- Les Luthiers
#3 lore raw

Hello again,

Quoted 8 lines · Pablo Neira Ayuso
Quoted 3 lines · Eric Leblond

Hello,

I attach a patch which is working for me.

Oh, I didn't notice this patch. I think that my patch results in a cleaner code. The current list handling is a bit error prone. What do you think?

I'm not really found of fixing it with a huge patch because libnfnetlink code is currently stable and because the code should not evolve much (All features are implemented and base netlink implementation is old and stable).

BR,

Signature · 3 lines
Eric Leblond
INL: http://www.inl.fr/
NuFW: http://www.nufw.org/