[jdev] PATCH: fix c2s authreg memory leaks

Cameron Moore lists at unbeatenpath.net
Sat Nov 20 01:58:03 CST 2004


Hello,
I've attached a small patch against the v2_0 cvs branch.  Here's the
change summary:

  * c2s/authreg_(my|pg)sql.c: tie in free function as soon as possible
    to prevent memory leaks
  * c2s/authreg.c: use authreg_free()

Now for the extended description.  I was playing around with valgrind
today and noticed that if I started c2s without mysqld running, it would
leak a little memory.  The problem was that in ar_mysql_init(), the
mysqlcontext object was never destroyed if the connection failed.  The
cleanest way to fix this is to let authreg_init() free it properly when
the module init fails.  To make this work, I moved the assignment of the
free function up to the top of ar_mysql_init() and fixed some improper
freeing stuff in the main authreg_init().  I haven't tested the patch to
authreg_pgsql.c, but I'm pretty sure it will work.

DISCLAIMER: I've stayed up way too late fixing this bug, and I'm not a C
guru anyway.  I'm sure I'll reread this over the weekend and see that I
screwed something up.  ;-)

PS - Anyone else seeing that when mysql is not running, that c2s doesn't
send any errors to syslog?  I haven't figured that one out yet...
-- 
Cameron Moore
[ Why is a boxing ring square? ]
-------------- next part --------------
Index: authreg.c
===================================================================
RCS file: /home/cvs/jabberd2/c2s/authreg.c,v
retrieving revision 1.40.2.11
diff -u -r1.40.2.11 authreg.c
--- authreg.c	30 Apr 2004 02:42:53 -0000	1.40.2.11
+++ authreg.c	20 Nov 2004 07:15:39 -0000
@@ -141,7 +141,7 @@
     if((init)(ar) != 0)
     {
         log_write(c2s->log, LOG_ERR, "failed to initialise auth module '%s'", name);
-        free(ar);
+        authreg_free(ar);
         return NULL;
     }
 
@@ -149,8 +149,7 @@
     if(ar->user_exists == NULL)
     {
         log_write(c2s->log, LOG_ERR, "auth module '%s' has no check for user existence", name);
-        if(ar->free != NULL) (ar->free)(ar);
-        free(ar);
+        authreg_free(ar);
         return NULL;
     }
     
Index: authreg_mysql.c
===================================================================
RCS file: /home/cvs/jabberd2/c2s/authreg_mysql.c,v
retrieving revision 1.8.2.4
diff -u -r1.8.2.4 authreg_mysql.c
--- authreg_mysql.c	31 May 2004 23:01:24 -0000	1.8.2.4
+++ authreg_mysql.c	20 Nov 2004 07:15:40 -0000
@@ -365,6 +365,7 @@
     /* configure the database context with field names and SQL statements */
     mysqlcontext = (mysqlcontext_t) malloc( sizeof( struct mysqlcontext_st ) );
     ar->private = mysqlcontext;
+    ar->free = _ar_mysql_free;
 
     /* determine our field names and table name */
     username = _ar_mysql_param( ar->c2s->config
@@ -500,7 +501,6 @@
     ar->set_zerok = _ar_mysql_set_zerok;
     ar->create_user = _ar_mysql_create_user;
     ar->delete_user = _ar_mysql_delete_user;
-    ar->free = _ar_mysql_free;
 
     return 0;
 }
Index: authreg_pgsql.c
===================================================================
RCS file: /home/cvs/jabberd2/c2s/authreg_pgsql.c,v
retrieving revision 1.11.2.3
diff -u -r1.11.2.3 authreg_pgsql.c
--- authreg_pgsql.c	31 May 2004 23:01:24 -0000	1.11.2.3
+++ authreg_pgsql.c	20 Nov 2004 07:15:40 -0000
@@ -365,6 +365,7 @@
     /* configure the database context with field names and SQL statements */
     pgsqlcontext = (pgsqlcontext_t) malloc( sizeof( struct pgsqlcontext_st ) );
     ar->private = pgsqlcontext;
+    ar->free = _ar_pgsql_free;
 
     /* determine our field names and table name */
     username = _ar_pgsql_param( ar->c2s->config
@@ -497,7 +498,6 @@
     ar->set_zerok = _ar_pgsql_set_zerok;
     ar->create_user = _ar_pgsql_create_user;
     ar->delete_user = _ar_pgsql_delete_user;
-    ar->free = _ar_pgsql_free;
 
     return 0;
 }


More information about the JDev mailing list