Fix bug #3150, "etpan_certificate_check() leaks memory"
[claws.git] / src / etpan / imap-thread.c
index 4a1f1fad12e1530d156e9d9f8bb4a040dab79712..ad5e3b3ef1c0158c0a784d3c20cdc5396715f6a3 100644 (file)
@@ -523,8 +523,8 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
 {
 #ifdef USE_GNUTLS
        struct connect_param *param = (struct connect_param *)data;
-       gnutls_x509_crt cert = NULL;
-       gnutls_datum tmp;
+       gnutls_x509_crt_t cert = NULL;
+       gnutls_datum_t tmp;
        
        if (certificate == NULL || len < 0) {
                g_warning("no cert presented.\n");
@@ -536,13 +536,16 @@ static int etpan_certificate_check(const unsigned char *certificate, int len, vo
        tmp.size = len;
        gnutls_x509_crt_init(&cert);
        if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
+               free(tmp.data);
                g_warning("IMAP: can't get cert\n");
                return 0;
        } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
                        (gushort)param->port) == TRUE) {
+               free(tmp.data);
                gnutls_x509_crt_deinit(cert);
                return 0;
        } else {
+               free(tmp.data);
                gnutls_x509_crt_deinit(cert);
                return -1;
        }
@@ -556,8 +559,8 @@ static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context,
        PrefsAccount *account = (PrefsAccount *)data;
        const gchar *cert_path = NULL;
        const gchar *password = NULL;
-       gnutls_x509_crt x509 = NULL;
-       gnutls_x509_privkey pkey = NULL;
+       gnutls_x509_crt_t x509 = NULL;
+       gnutls_x509_privkey_t pkey = NULL;
 
        if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
                cert_path = account->in_ssl_client_cert_file;
@@ -1553,6 +1556,7 @@ int imap_threaded_examine(Folder * folder, const char * mb,
 struct search_param {
        mailimap * imap;
        int type;
+       const char *charset;
        struct mailimap_set * set;
        IMAPSearchKey* key;
 };
@@ -1591,7 +1595,6 @@ static void search_run(struct etpan_thread_op * op)
        struct mailimap_search_key * uid_key = NULL;
        struct mailimap_search_key * search_type_key = NULL;
        clist * search_result;
-       static const char *charset = NULL;
 
        param = op->param;
        result = op->result;
@@ -1630,7 +1633,6 @@ static void search_run(struct etpan_thread_op * op)
                search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
                break;
        case IMAP_SEARCH_TYPE_KEYED:
-               charset = "UTF-8";
                search_type_key = param->key;
                break;
        }
@@ -1655,7 +1657,7 @@ static void search_run(struct etpan_thread_op * op)
        } else {
                mailstream_logger = imap_logger_uid;
 
-               r = mailimap_uid_search(param->imap, charset, key, &search_result);
+               r = mailimap_uid_search(param->imap, param->charset, key, &search_result);
 
                mailstream_logger = imap_logger_cmd;
 
@@ -1669,7 +1671,8 @@ static void search_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
-                        struct mailimap_set * set, clist ** search_result)
+                        const char *charset, struct mailimap_set * set,
+                        clist ** search_result)
 {
        struct search_param param;
        struct search_result result;
@@ -1680,6 +1683,7 @@ int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
        imap = get_imap(folder);
        param.imap = imap;
        param.set = set;
+       param.charset = charset;
        param.type = search_type;
        param.key = key;