Update of /usr/cvsroot/asterisk/cdr
In directory mongoose.digium.com:/tmp/cvs-serv11280/cdr

Modified Files:
        cdr_odbc.c cdr_pgsql.c 
Log Message:
remove complex malloc-avoidance (bug #4601)
remove resetting of variables during unload that will only be freed or set to 
known values on reload


Index: cdr_odbc.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cdr/cdr_odbc.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -d -r1.28 -r1.29
--- cdr_odbc.c  6 Jun 2005 22:12:18 -0000       1.28
+++ cdr_odbc.c  11 Jul 2005 21:06:27 -0000      1.29
@@ -40,7 +40,6 @@
 static char *name = "ODBC";
 static char *config = "cdr_odbc.conf";
 static char *dsn = NULL, *username = NULL, *password = NULL, *table = NULL;
-static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, table_alloc 
= 0;
 static int loguniqueid = 0;
 static int usegmtime = 0;
 static int dispositionstring = 0;
@@ -191,39 +190,27 @@
                SQLDisconnect(ODBC_con);
                SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con);
                SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env);
-               connected = 0;
        }
-       if (dsn && dsn_alloc) {
+       if (dsn) {
                if (option_verbose > 10)
                        ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free dsn\n");
                free(dsn);
-               dsn = NULL;
-               dsn_alloc = 0;
        }
-       if (username && username_alloc) {
+       if (username) {
                if (option_verbose > 10)
                        ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free 
username\n");
                free(username);
-               username = NULL;
-               username_alloc = 0;
        }
-       if (password && password_alloc) {
+       if (password) {
                if (option_verbose > 10)
                        ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free 
password\n");
                free(password);
-               password = NULL;
-               password_alloc = 0;
        }
-       if (table && table_alloc) {
+       if (table) {
                if (option_verbose > 10)
                        ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
                free(table);
-               table = NULL;
-               table_alloc = 0;
        }
-       loguniqueid = 0;
-       usegmtime = 0;
-       dispositionstring = 0;
 
        ast_cdr_unregister(name);
        ast_mutex_unlock(&odbc_lock);
@@ -252,20 +239,15 @@
        }
 
        tmp = ast_variable_retrieve(cfg,"global","dsn");
-       if (tmp) {
-               dsn = malloc(strlen(tmp) + 1);
-               if (dsn != NULL) {
-                       memset(dsn, 0, strlen(tmp) + 1);
-                       dsn_alloc = 1;
-                       strncpy(dsn, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
-                       res = -1;
-                       goto out;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"cdr_odbc: dsn not specified.  Assuming 
asteriskdb\n");
-               dsn = "asteriskdb";
+               tmp = "asteriskdb";
+       }
+       dsn = strdup(tmp);
+       if (dsn == NULL) {
+               ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
+               res = -1;
+               goto out;
        }
 
        tmp = ast_variable_retrieve(cfg,"global","dispositionstring");
@@ -277,12 +259,8 @@
                
        tmp = ast_variable_retrieve(cfg,"global","username");
        if (tmp) {
-               username = malloc(strlen(tmp) + 1);
-               if (username != NULL) {
-                       memset(username, 0, strlen(tmp) + 1);
-                       username_alloc = 1;
-                       strncpy(username, tmp, strlen(tmp));
-               } else {
+               username = strdup(tmp);
+               if (username == NULL) {
                        ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
                        res = -1;
                        goto out;
@@ -291,12 +269,8 @@
 
        tmp = ast_variable_retrieve(cfg,"global","password");
        if (tmp) {
-               password = malloc(strlen(tmp) + 1);
-               if (password != NULL) {
-                       memset(password, 0, strlen(tmp) + 1);
-                       password_alloc = 1;
-                       strncpy(password, tmp, strlen(tmp));
-               } else {
+               password = strdup(tmp);
+               if (password == NULL) {
                        ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
                        res = -1;
                        goto out;
@@ -330,20 +304,15 @@
        }
 
        tmp = ast_variable_retrieve(cfg,"global","table");
-       if (tmp) {
-               table = malloc(strlen(tmp) + 1);
-               if (table != NULL) {
-                       memset(table, 0, strlen(tmp) + 1);
-                       table_alloc = 1;
-                       strncpy(table, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
-                       res = -1;
-                       goto out;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"cdr_odbc: table not specified.  Assuming 
cdr\n");
-               table = "cdr";
+               tmp = "cdr";
+       }
+       table = strdup(tmp);
+       if (table == NULL) {
+               ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
+               res = -1;
+               goto out;
        }
 
        ast_config_destroy(cfg);
@@ -437,6 +406,7 @@
                SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER 
*)10, 0);    
        }
 
+       /* XXX note username and password could be NULL here */
        ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)dsn, SQL_NTS, 
(SQLCHAR*)username, SQL_NTS, (SQLCHAR*)password, SQL_NTS);
 
        if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {

Index: cdr_pgsql.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cdr/cdr_pgsql.c,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- cdr_pgsql.c 6 Jun 2005 22:12:18 -0000       1.19
+++ cdr_pgsql.c 11 Jul 2005 21:06:27 -0000      1.20
@@ -42,7 +42,6 @@
 static char *name = "pgsql";
 static char *config = "cdr_pgsql.conf";
 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, 
*pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL, *table = NULL;
-static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, 
password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0, table_alloc = 0;
 static int connected = 0;
 
 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
@@ -166,43 +165,20 @@
 { 
        if (conn)
                PQfinish(conn);
-       conn = NULL;
-       connected = 0;
-       if (pghostname && hostname_alloc) {
+       if (pghostname)
                free(pghostname);
-               pghostname = NULL;
-               hostname_alloc = 0;
-       }
-       if (pgdbname && dbname_alloc) {
+       if (pgdbname)
                free(pgdbname);
-               pgdbname = NULL;
-               dbname_alloc = 0;
-       }
-       if (pgdbuser && dbuser_alloc) {
+       if (pgdbuser)
                free(pgdbuser);
-               pgdbuser = NULL;
-               dbuser_alloc = 0;
-       }
-       if (pgdbsock && dbsock_alloc) {
+       if (pgdbsock)
                free(pgdbsock);
-               pgdbsock = NULL;
-               dbsock_alloc = 0;
-       }
-       if (pgpassword && password_alloc) {
+       if (pgpassword)
                free(pgpassword);
-               pgpassword = NULL;
-               password_alloc = 0;
-       }
-       if (pgdbport && dbport_alloc) {
+       if (pgdbport)
                free(pgdbport);
-               pgdbport = NULL;
-               dbport_alloc = 0;
-       }
-       if (table && table_alloc) {
+       if (table)
                free(table);
-               table = NULL;
-               table_alloc = 0;
-       }
        ast_cdr_unregister(name);
        return 0;
 }
@@ -221,99 +197,69 @@
        }
 
        tmp = ast_variable_retrieve(cfg,"global","hostname");
-       if (tmp) {
-               pghostname = malloc(strlen(tmp) + 1);
-               if (pghostname != NULL) {
-                       memset(pghostname, 0, strlen(tmp) + 1);
-                       hostname_alloc = 1;
-                       strncpy(pghostname, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  
Assuming localhost\n");
-               pghostname = "localhost";
+               tmp = "localhost";
+       }
+       pghostname = strdup(tmp);
+       if (pghostname == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
        }
 
        tmp = ast_variable_retrieve(cfg,"global","dbname");
-       if (tmp) {
-               pgdbname = malloc(strlen(tmp) + 1);
-               if (pgdbname != NULL) {
-                       memset(pgdbname, 0, strlen(tmp) + 1);
-                       dbname_alloc = 1;
-                       strncpy(pgdbname, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"PostgreSQL database not specified.  
Assuming asterisk\n");
-               pgdbname = "asteriskcdrdb";
+               tmp = "asteriskcdrdb";
+       }
+       pgdbname = strdup(tmp);
+       if (pgdbname == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
        }
 
        tmp = ast_variable_retrieve(cfg,"global","user");
-       if (tmp) {
-               pgdbuser = malloc(strlen(tmp) + 1);
-               if (pgdbuser != NULL) {
-                       memset(pgdbuser, 0, strlen(tmp) + 1);
-                       dbuser_alloc = 1;
-                       strncpy(pgdbuser, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"PostgreSQL database user not specified.  
Assuming root\n");
-               pgdbuser = "root";
+               tmp = "root";
+       }
+       pgdbuser = strdup(tmp);
+       if (pgdbuser == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
        }
 
        tmp = ast_variable_retrieve(cfg,"global","password");
-       if (tmp) {
-               pgpassword = malloc(strlen(tmp) + 1);
-               if (pgpassword != NULL) {
-                       memset(pgpassword, 0, strlen(tmp) + 1);
-                       password_alloc = 1;
-                       strncpy(pgpassword, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"PostgreSQL database password not 
specified.  Assuming blank\n");
-               pgpassword = "";
+               tmp = "";
+       }
+       pgpassword = strdup(tmp);
+       if (pgpassword == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
        }
 
        tmp = ast_variable_retrieve(cfg,"global","port");
-       if (tmp) {
-               pgdbport = malloc(strlen(tmp) + 1);
-               if (pgdbport != NULL) {
-                       memset(pgdbport, 0, strlen(tmp) + 1);
-                       dbport_alloc = 1;
-                       strncpy(pgdbport, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"PostgreSQL database port not specified.  
Using default 5432.\n");
-               pgdbport = "5432";
+               tmp = "5432";
        }
-        /* Loading stuff for table name */
+       pgdbport = strdup(tmp);
+       if (pgdbport == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
+       }
+
        tmp = ast_variable_retrieve(cfg,"global","table");
-       if (tmp) {
-               table = malloc(strlen(tmp) + 1);
-               if (table != NULL) {
-                       memset(table, 0, strlen(tmp) + 1);
-                       table_alloc = 1;
-                       strncpy(table, tmp, strlen(tmp));
-               } else {
-                       ast_log(LOG_ERROR,"Out of memory error.\n");
-                       return -1;
-               }
-       } else {
+       if (tmp == NULL) {
                ast_log(LOG_WARNING,"CDR table not specified.  Assuming cdr\n");
-               table = "cdr";
+               tmp = "cdr";
+       }
+       table = strdup(tmp);
+       if (table == NULL) {
+               ast_log(LOG_ERROR,"Out of memory error.\n");
+               return -1;
        }
 
        ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);

_______________________________________________
Asterisk-Cvs mailing list
[email protected]
http://lists.digium.com/mailman/listinfo/asterisk-cvs

Reply via email to