Fix for very unlikely memory leak in res_odbc
authorMatthew Fredrickson <creslin@digium.com>
Thu, 2 Feb 2006 16:09:01 +0000 (16:09 +0000)
committerMatthew Fredrickson <creslin@digium.com>
Thu, 2 Feb 2006 16:09:01 +0000 (16:09 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@9071 65c4cc65-6c06-0410-ace0-fbb531ad65f3

res/res_odbc.c

index 3f255f0..f373a99 100644 (file)
@@ -430,39 +430,41 @@ odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
 {
        static odbc_obj *new;
 
-       new = malloc(sizeof(odbc_obj));
-       if (!new)
-               return NULL;
-       memset(new, 0, sizeof(odbc_obj));
-       new->env = SQL_NULL_HANDLE;
-
-       new->name = malloc(strlen(name) + 1);
-       if (new->name == NULL)
-               return NULL;
-
-       new->dsn = malloc(strlen(dsn) + 1);
-       if (new->dsn == NULL)
-               return NULL;
+       if (!(new = calloc(1, sizeof(*new))) || 
+           !(new->name = malloc(strlen(name) + 1)) || 
+           !(new->dsn = malloc(strlen(dsn) + 1)))
+               goto cleanup;
 
        if (username) {
-               new->username = malloc(strlen(username) + 1);
-               if (new->username == NULL)
-                       return NULL;
+               if (!(new->username = malloc(strlen(username) + 1)))
+                       goto cleanup;
                strcpy(new->username, username);
        }
 
        if (password) {
-               new->password = malloc(strlen(password) + 1);
-               if (new->password == NULL)
-                       return NULL;
+               if (!(new->password = malloc(strlen(password) + 1)))
+                       goto cleanup;
                strcpy(new->password, password);
        }
 
        strcpy(new->name, name);
        strcpy(new->dsn, dsn);
+       new->env = SQL_NULL_HANDLE;
        new->up = 0;
        ast_mutex_init(&new->lock);
        return new;
+
+cleanup:
+       if (new) {
+               free(new->name);
+               free(new->dsn);
+               free(new->username);
+               free(new->password);
+
+               free(new);      
+       }
+
+       return NULL;
 }
 
 void destroy_odbc_obj(odbc_obj **obj)