From 919bfa717833e8a5775d5214af09908a157f81b1 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 23 Apr 2023 02:11:39 +0800 Subject: [PATCH] [import-commodity-matcher.c] plug GList leaks commodity_list and namespace_list were NULL by the time they were freed. --- .../import-export/import-commodity-matcher.c | 49 ++++++------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/gnucash/import-export/import-commodity-matcher.c b/gnucash/import-export/import-commodity-matcher.c index d34904ef46..12b6b1d7c5 100644 --- a/gnucash/import-export/import-commodity-matcher.c +++ b/gnucash/import-export/import-commodity-matcher.c @@ -57,54 +57,33 @@ gnc_commodity * gnc_import_select_commodity(const char * cusip, { const gnc_commodity_table * commodity_table = gnc_get_current_commodities (); gnc_commodity * retval = NULL; - gnc_commodity * tmp_commodity = NULL; - char * tmp_namespace = NULL; - GList * commodity_list = NULL; - GList * namespace_list = NULL; - DEBUG("Default fullname received: %s", - default_fullname ? default_fullname : "(null)"); - DEBUG("Default mnemonic received: %s", - default_mnemonic ? default_mnemonic : "(null)"); + DEBUG("Default fullname received: %s", default_fullname); + DEBUG("Default mnemonic received: %s", default_mnemonic); g_return_val_if_fail(cusip, NULL); DEBUG("Looking for commodity with exchange_code: %s", cusip); g_assert(commodity_table); - namespace_list = gnc_commodity_table_get_namespaces(commodity_table); + GList *namespace_list = gnc_commodity_table_get_namespaces(commodity_table); - - namespace_list = g_list_first(namespace_list); - while ( namespace_list != NULL && retval == NULL) + for (GList *n = namespace_list; !retval && n; n = g_list_next (n)) { - tmp_namespace = namespace_list->data; - DEBUG("Looking at namespace %s", tmp_namespace); - commodity_list = gnc_commodity_table_get_commodities(commodity_table, - tmp_namespace); - commodity_list = g_list_first(commodity_list); - while ( commodity_list != NULL && retval == NULL) + const char *ns = n->data; + DEBUG("Looking at namespace %s", ns); + GList *comm_list = gnc_commodity_table_get_commodities (commodity_table, ns); + for (GList *m = comm_list; !retval && m; m = g_list_next (m)) { - const char* tmp_cusip = NULL; - tmp_commodity = commodity_list->data; - DEBUG("Looking at commodity %s", - gnc_commodity_get_fullname(tmp_commodity)); - tmp_cusip = gnc_commodity_get_cusip(tmp_commodity); - if (tmp_cusip != NULL && cusip != NULL) + gnc_commodity *com = comm_list->data; + DEBUG("Looking at commodity %s", gnc_commodity_get_fullname (com)); + if (!g_strcmp0 (gnc_commodity_get_cusip (com), cusip)) { - int len = strlen(cusip) > strlen(tmp_cusip) ? strlen(cusip) : - strlen(tmp_cusip); - if (strncmp(tmp_cusip, cusip, len) == 0) - { - retval = tmp_commodity; - DEBUG("Commodity %s%s", - gnc_commodity_get_fullname(retval), " matches."); - } + retval = com; + DEBUG("Commodity %s matches.", gnc_commodity_get_fullname (com)); } - commodity_list = g_list_next(commodity_list); } - namespace_list = g_list_next(namespace_list); + g_list_free (comm_list); } - g_list_free(commodity_list); g_list_free(namespace_list); if (retval == NULL && ask_on_unknown != 0)