From f95fe9a31a7b95d619dc9345ca21c1d030257abe Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Tue, 4 Nov 2014 13:40:34 +0000 Subject: [PATCH] Fix up some more memory leaks Signed-off-by: David Woodhouse --- http.c | 5 +++-- library.c | 6 +++++- openconnect-internal.h | 15 +++++++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/http.c b/http.c index 4f6eed10..c8eae16f 100644 --- a/http.c +++ b/http.c @@ -972,8 +972,8 @@ static int handle_redirect(struct openconnect_info *vpninfo) openconnect_close_https(vpninfo, 0); openconnect_clear_cookies(vpninfo); vpninfo->redirect_type = REDIR_TYPE_NEWHOST; - } else - free(host); + } + free(host); free(vpninfo->redirect_url); vpninfo->redirect_url = NULL; @@ -1269,6 +1269,7 @@ int openconnect_obtain_cookie(struct openconnect_info *vpninfo) method = "GET"; if (orig_host) { openconnect_set_hostname(vpninfo, orig_host); + free(orig_host); orig_host = NULL; free(vpninfo->urlpath); vpninfo->urlpath = orig_path; diff --git a/library.c b/library.c index 7c3a1272..2606782b 100644 --- a/library.c +++ b/library.c @@ -185,6 +185,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo) free_optlist(vpninfo->dtls_options); cstp_free_splits(vpninfo); free(vpninfo->hostname); + free(vpninfo->unique_hostname); free(vpninfo->urlpath); free(vpninfo->redirect_url); free(vpninfo->cookie); @@ -277,7 +278,6 @@ int openconnect_set_hostname(struct openconnect_info *vpninfo, { UTF8CHECK(hostname); - free(vpninfo->hostname); STRDUP(vpninfo->hostname, hostname); free(vpninfo->unique_hostname); vpninfo->unique_hostname = NULL; @@ -384,6 +384,10 @@ int openconnect_set_client_cert(struct openconnect_info *vpninfo, UTF8CHECK(cert); UTF8CHECK(sslkey); + /* Avoid freeing it twice if it's the same */ + if (vpninfo->sslkey == vpninfo->cert) + vpninfo->sslkey = NULL; + STRDUP(vpninfo->cert, cert); if (sslkey) { diff --git a/openconnect-internal.h b/openconnect-internal.h index de87defa..0a7d1ff5 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -236,8 +236,8 @@ struct openconnect_info { int port; char *urlpath; int cert_expire_warning; - const char *cert; - const char *sslkey; + char *cert; + char *sslkey; char *cert_password; char *cafile; unsigned no_system_trust; @@ -724,10 +724,13 @@ int digest_authorization(struct openconnect_info *vpninfo, struct oc_text_buf *b extern const char *openconnect_version_str; #define STRDUP(res, arg) \ - if (arg) { \ - res = strdup(arg); \ - if (res == NULL) return -ENOMEM; \ - } else res = NULL + do { \ + free(res); \ + if (arg) { \ + res = strdup(arg); \ + if (res == NULL) return -ENOMEM; \ + } else res = NULL; \ + } while(0) #define UTF8CHECK(arg) \ if ((arg) && buf_append_utf16le(NULL, (arg))) { \