From: Marcel Poul Date: Tue, 13 Nov 2012 00:38:15 +0000 (+0000) Subject: Properly free memory allocated for canl_ctx, fix some memory leaks X-Git-Tag: emi-canl-c_R_2_0_2_1~4 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=39dedf3fe7efc78e3c30e6677d78cdbee647f5d3;p=jra1mw.git Properly free memory allocated for canl_ctx, fix some memory leaks --- diff --git a/emi.canl.canl-c/src/canl.c b/emi.canl.canl-c/src/canl.c index c254e38..dac745a 100644 --- a/emi.canl.canl-c/src/canl.c +++ b/emi.canl.canl-c/src/canl.c @@ -37,6 +37,7 @@ canl_ctx canl_create_ctx() void canl_free_ctx(canl_ctx cc) { glb_ctx *ctx = (glb_ctx*) cc; + struct canl_mech *mech = find_mech(GSS_C_NO_OID); if (!cc) return; @@ -47,10 +48,17 @@ void canl_free_ctx(canl_ctx cc) ctx->err_msg = NULL; } /*TODO delete ctx content for real*/ + if (mech) + mech->free_ctx(ctx); + if (ctx->err_msg){ + free(ctx->err_msg); + ctx->err_msg = NULL; + } free(ctx); } + canl_err_code canl_create_io_handler(canl_ctx cc, canl_io_handler *io) { @@ -172,7 +180,7 @@ canl_io_connect(canl_ctx cc, canl_io_handler io, const char *host, err = mech->connect(glb_cc, io_cc, ctx, timeout, host); if (err) { canl_io_close(glb_cc, io_cc); - mech->free_ctx(glb_cc, ctx); + mech->finish(glb_cc, ctx); ctx = NULL; continue; } @@ -361,7 +369,7 @@ end: if (err) { (io_cc)->sock = -1; if (conn_ctx) - mech->free_ctx(glb_cc, conn_ctx); + mech->finish(glb_cc, conn_ctx); } return err; @@ -408,7 +416,7 @@ static void io_destroy(glb_ctx *cc, io_handler *io) if (io_cc->conn_ctx) { mech = find_mech(io->oid); - mech->free_ctx(cc, io_cc->conn_ctx); + mech->finish(cc, io_cc->conn_ctx); io_cc->conn_ctx = NULL; io_cc->oid = GSS_C_NO_OID; } diff --git a/emi.canl.canl-c/src/canl_locl.h b/emi.canl.canl-c/src/canl_locl.h index d253efc..a516ab1 100644 --- a/emi.canl.canl-c/src/canl_locl.h +++ b/emi.canl.canl-c/src/canl_locl.h @@ -94,7 +94,7 @@ typedef struct canl_mech { (glb_ctx *, void **); canl_err_code (*free_ctx) - (glb_ctx *, void *); + (glb_ctx *); canl_err_code (*connect) (glb_ctx *, io_handler *, void *, struct timeval *, const char *); diff --git a/emi.canl.canl-c/src/canl_ssl.c b/emi.canl.canl-c/src/canl_ssl.c index 6b1a5ce..1659deb 100644 --- a/emi.canl.canl-c/src/canl_ssl.c +++ b/emi.canl.canl-c/src/canl_ssl.c @@ -1056,16 +1056,53 @@ ssl_close(glb_ctx *cc, io_handler *io, void *auth_ctx) } static canl_err_code -ssl_free(glb_ctx *cc, void *ctx) +ssl_finish(glb_ctx *cc, void *ctx) { SSL_free(ctx); return 0; } static canl_err_code -ssl_finish(glb_ctx *cc, void *ctx) +ssl_free_ctx(glb_ctx *cc) { - SSL_CTX_free(ctx); + mech_glb_ctx *m_ctx = cc->mech_ctx; + SSL_CTX_free(m_ctx->mech_ctx); + m_ctx->mech_ctx = NULL; + + if (!m_ctx) + return 0; + + if (m_ctx->ca_dir){ + free(m_ctx->ca_dir); + m_ctx->ca_dir = NULL; + } + if (m_ctx->ca_file){ + free(m_ctx->ca_file); + m_ctx->ca_file = NULL; + } + if (m_ctx->crl_dir){ + free(m_ctx->crl_dir); + m_ctx->crl_dir = NULL; + } + + if (m_ctx->cert_key){ + if (m_ctx->cert_key->cert){ + X509_free(m_ctx->cert_key->cert); + m_ctx->cert_key->cert = NULL; + } + if (m_ctx->cert_key->key){ + EVP_PKEY_free(m_ctx->cert_key->key); + m_ctx->cert_key->key = NULL; + } + if (m_ctx->cert_key->chain){ + sk_X509_pop_free(m_ctx->cert_key->chain, X509_free); + m_ctx->cert_key->chain = NULL; + } + free(m_ctx->cert_key); + m_ctx->cert_key = NULL; + } + free(m_ctx); + cc->mech_ctx = NULL; return 0; } @@ -1310,7 +1347,7 @@ canl_mech canl_mech_ssl = { ssl_finish, ssl_client_init, ssl_server_init, - ssl_free, + ssl_free_ctx, ssl_connect, ssl_accept, ssl_close,