Clean up memory management when loading IMC/IMVs from files
authorMartin Willi <martin@revosec.ch>
Wed, 14 Nov 2012 10:17:36 +0000 (11:17 +0100)
committerMartin Willi <martin@revosec.ch>
Fri, 30 Nov 2012 14:47:34 +0000 (15:47 +0100)
src/libcharon/plugins/tnc_imc/tnc_imc.c
src/libcharon/plugins/tnc_imc/tnc_imc_manager.c
src/libcharon/plugins/tnc_imv/tnc_imv.c
src/libcharon/plugins/tnc_imv/tnc_imv_manager.c
src/libtnccs/tnc/tnc.c

index 16d822d..5210650 100644 (file)
@@ -38,11 +38,6 @@ struct private_tnc_imc_t {
        imc_t public;
 
        /**
-        * Path of loaded IMC
-        */
-       char *path;
-
-       /**
         * Name of loaded IMC
         */
        char *name;
@@ -291,10 +286,10 @@ METHOD(imc_t, type_supported, bool,
 
        for (i = 0; i < this->type_count; i++)
        {
-           vid = this->supported_vids[i];
-           subtype = this->supported_subtypes[i];
+               vid = this->supported_vids[i];
+               subtype = this->supported_subtypes[i];
 
-           if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
+               if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
                        (vid == msg_vid && (subtype == TNC_SUBTYPE_ANY ||
                         subtype == msg_subtype)))
                {
@@ -307,13 +302,15 @@ METHOD(imc_t, type_supported, bool,
 METHOD(imc_t, destroy, void,
        private_tnc_imc_t *this)
 {
-       dlclose(this->handle);
+       if (this->handle)
+       {
+               dlclose(this->handle);
+       }
        this->mutex->destroy(this->mutex);
        this->additional_ids->destroy(this->additional_ids);
        free(this->supported_vids);
        free(this->supported_subtypes);
        free(this->name);
-       free(this->path);
        free(this);
 }
 
@@ -335,9 +332,8 @@ imc_t* tnc_imc_create(char *name, char *path)
                        .set_message_types_long = _set_message_types_long,
                        .type_supported = _type_supported,
                        .destroy = _destroy,
-        },
-               .name = name,
-               .path = path,
+               },
+               .name = strdup(name),
                .additional_ids = linked_list_create(),
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
        );
@@ -346,46 +342,43 @@ imc_t* tnc_imc_create(char *name, char *path)
        if (!this->handle)
        {
                DBG1(DBG_TNC, "IMC \"%s\" failed to load: %s", name, dlerror());
-               free(this);
+               destroy(this);
                return NULL;
        }
 
        this->public.initialize = dlsym(this->handle, "TNC_IMC_Initialize");
        if (!this->public.initialize)
-    {
+       {
                DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n",
                                           path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
        this->public.notify_connection_change =
                                                 dlsym(this->handle, "TNC_IMC_NotifyConnectionChange");
-    this->public.begin_handshake = dlsym(this->handle, "TNC_IMC_BeginHandshake");
+       this->public.begin_handshake = dlsym(this->handle, "TNC_IMC_BeginHandshake");
        if (!this->public.begin_handshake)
-    {
+       {
                DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
                                           path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
-    this->public.receive_message =
+       this->public.receive_message =
                                                dlsym(this->handle, "TNC_IMC_ReceiveMessage");
-    this->public.receive_message_long =
+       this->public.receive_message_long =
                                                dlsym(this->handle, "TNC_IMC_ReceiveMessageLong");
-    this->public.batch_ending =
+       this->public.batch_ending =
                                                dlsym(this->handle, "TNC_IMC_BatchEnding");
-    this->public.terminate =
+       this->public.terminate =
                                                dlsym(this->handle, "TNC_IMC_Terminate");
-    this->public.provide_bind_function =
+       this->public.provide_bind_function =
                                                dlsym(this->handle, "TNC_IMC_ProvideBindFunction");
-    if (!this->public.provide_bind_function)
+       if (!this->public.provide_bind_function)
        {
                DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
                                          path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
 
index fcbeec6..bc929b8 100644 (file)
@@ -103,8 +103,6 @@ METHOD(imc_manager_t, load, bool,
        imc = tnc_imc_create(name, path);
        if (!imc)
        {
-               free(name);
-               free(path);
                return FALSE;
        }
        if (!add(this, imc))
index a71ffc0..567756e 100644 (file)
@@ -38,11 +38,6 @@ struct private_tnc_imv_t {
        imv_t public;
 
        /**
-        * Path of loaded IMV
-        */
-       char *path;
-
-       /**
         * Name of loaded IMV
         */
        char *name;
@@ -287,10 +282,10 @@ METHOD(imv_t, type_supported, bool,
 
        for (i = 0; i < this->type_count; i++)
        {
-           vid = this->supported_vids[i];
-           subtype = this->supported_subtypes[i];
+               vid = this->supported_vids[i];
+               subtype = this->supported_subtypes[i];
 
-           if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
+               if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
                        (vid == msg_vid && (subtype == TNC_SUBTYPE_ANY ||
                         subtype == msg_subtype)))
                {
@@ -303,13 +298,15 @@ METHOD(imv_t, type_supported, bool,
 METHOD(imv_t, destroy, void,
        private_tnc_imv_t *this)
 {
-       dlclose(this->handle);
+       if (this->handle)
+       {
+               dlclose(this->handle);
+       }
        this->mutex->destroy(this->mutex);
        this->additional_ids->destroy_function(this->additional_ids, free);
        free(this->supported_vids);
        free(this->supported_subtypes);
        free(this->name);
-       free(this->path);
        free(this);
 }
 
@@ -332,8 +329,7 @@ imv_t* tnc_imv_create(char *name, char *path)
                        .type_supported = _type_supported,
                        .destroy = _destroy,
                },
-               .name = name,
-               .path = path,
+               .name = strdup(name),
                .additional_ids = linked_list_create(),
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
        );
@@ -342,7 +338,7 @@ imv_t* tnc_imv_create(char *name, char *path)
        if (!this->handle)
        {
                DBG1(DBG_TNC, "IMV \"%s\" failed to load: %s", name, dlerror());
-               free(this);
+               destroy(this);
                return NULL;
        }
 
@@ -351,8 +347,7 @@ imv_t* tnc_imv_create(char *name, char *path)
        {
                DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n",
                                           path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
        this->public.notify_connection_change =
@@ -363,8 +358,7 @@ imv_t* tnc_imv_create(char *name, char *path)
        {
                DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
                                           path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
        this->public.receive_message =
@@ -381,8 +375,7 @@ imv_t* tnc_imv_create(char *name, char *path)
        {
                DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
                                          path, dlerror());
-               dlclose(this->handle);
-               free(this);
+               destroy(this);
                return NULL;
        }
 
index 21b7737..a8c0c03 100644 (file)
@@ -119,8 +119,6 @@ METHOD(imv_manager_t, load, bool,
        imv = tnc_imv_create(name, path);
        if (!imv)
        {
-               free(name);
-               free(path);
                return FALSE;
        }
        if (!add(this, imv))
index 728d2b2..9dca25a 100644 (file)
@@ -163,9 +163,7 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
                }
 
                /* copy the IMC/IMV name */
-               name = malloc(token.len + 1);
-               memcpy(name, token.ptr, token.len);
-               name[token.len] = '\0';
+               name = strndup(token.ptr, token.len);
 
                /* advance to the IMC/IMV path and extract it */
                if (!eat_whitespace(&line))
@@ -180,9 +178,7 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
                }
 
                /* copy the IMC/IMV path */
-               path = malloc(token.len + 1);
-               memcpy(path, token.ptr, token.len);
-               path[token.len] = '\0';
+               path = strndup(token.ptr, token.len);
 
                /* load and register an IMC/IMV instance */
                if (is_imc)
@@ -193,6 +189,8 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
                {
                        success = tnc->imvs->load(tnc->imvs, name, path);
                }
+               free(name);
+               free(path);
                if (!success)
                {
                        break;