From: Martin Willi Date: Wed, 14 Nov 2012 10:17:36 +0000 (+0100) Subject: Clean up memory management when loading IMC/IMVs from files X-Git-Tag: 5.0.2dr4~135 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd7495946552ab716380f587831d89da1392db1e;p=thirdparty%2Fstrongswan.git Clean up memory management when loading IMC/IMVs from files --- diff --git a/src/libcharon/plugins/tnc_imc/tnc_imc.c b/src/libcharon/plugins/tnc_imc/tnc_imc.c index 16d822d3b4..5210650141 100644 --- a/src/libcharon/plugins/tnc_imc/tnc_imc.c +++ b/src/libcharon/plugins/tnc_imc/tnc_imc.c @@ -37,11 +37,6 @@ struct private_tnc_imc_t { */ imc_t public; - /** - * Path of loaded IMC - */ - char *path; - /** * Name of loaded IMC */ @@ -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; } diff --git a/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c b/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c index fcbeec6eb9..bc929b8223 100644 --- a/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c +++ b/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c @@ -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)) diff --git a/src/libcharon/plugins/tnc_imv/tnc_imv.c b/src/libcharon/plugins/tnc_imv/tnc_imv.c index a71ffc0e04..567756ea18 100644 --- a/src/libcharon/plugins/tnc_imv/tnc_imv.c +++ b/src/libcharon/plugins/tnc_imv/tnc_imv.c @@ -37,11 +37,6 @@ struct private_tnc_imv_t { */ imv_t public; - /** - * Path of loaded IMV - */ - char *path; - /** * Name of loaded IMV */ @@ -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; } diff --git a/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c b/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c index 21b7737c31..a8c0c03901 100644 --- a/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c +++ b/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c @@ -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)) diff --git a/src/libtnccs/tnc/tnc.c b/src/libtnccs/tnc/tnc.c index 728d2b28d0..9dca25a002 100644 --- a/src/libtnccs/tnc/tnc.c +++ b/src/libtnccs/tnc/tnc.c @@ -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;