gnucash stable: [gnc-commodity.h] callers must free g_list
Christopher Lam
clam at code.gnucash.org
Tue Aug 27 08:31:36 EDT 2024
Updated via https://github.com/Gnucash/gnucash/commit/a99491d0 (commit)
from https://github.com/Gnucash/gnucash/commit/0fc6e4f7 (commit)
commit a99491d03425a3565bf2a2de6d3dbb8574a04815
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Sat Aug 24 13:55:11 2024 +0800
[gnc-commodity.h] callers must free g_list
callers to gnc_commodity_namespace_get_commodity_list and
gnc_commodity_table_get_namespaces_list must free the GList.
diff --git a/gnucash/gnome-utils/gnc-tree-model-commodity.c b/gnucash/gnome-utils/gnc-tree-model-commodity.c
index fe80392458..5c55929622 100644
--- a/gnucash/gnome-utils/gnc-tree-model-commodity.c
+++ b/gnucash/gnome-utils/gnc-tree-model-commodity.c
@@ -422,6 +422,7 @@ gnc_tree_model_commodity_get_iter (GtkTreeModel *tree_model,
list = gnc_commodity_table_get_namespaces_list(ct);
i = gtk_tree_path_get_indices (path)[0];
name_space = g_list_nth_data (list, i);
+ g_list_free (list);
if (!name_space)
{
LEAVE("invalid path at namespace");
@@ -443,6 +444,7 @@ gnc_tree_model_commodity_get_iter (GtkTreeModel *tree_model,
list = gnc_commodity_namespace_get_commodity_list(name_space);
i = gtk_tree_path_get_indices (path)[1];
commodity = g_list_nth_data (list, i);
+ g_list_free (list);
if (!commodity)
{
LEAVE("invalid path at commodity");
@@ -503,6 +505,7 @@ gnc_tree_model_commodity_get_path (GtkTreeModel *tree_model,
gtk_tree_path_append_index (path, g_list_index (ns_list, name_space));
gtk_tree_path_append_index (path, GPOINTER_TO_INT(iter->user_data3));
debug_path(LEAVE, path);
+ g_list_free (ns_list);
return path;
}
@@ -671,6 +674,7 @@ gnc_tree_model_commodity_iter_next (GtkTreeModel *tree_model,
n = GPOINTER_TO_INT(iter->user_data3) + 1;
iter->user_data2 = g_list_nth_data(list, n);
+ g_list_free (list);
if (iter->user_data2 == NULL)
{
LEAVE("no next iter");
@@ -713,6 +717,7 @@ gnc_tree_model_commodity_iter_children (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, 0);
iter->user_data3 = GINT_TO_POINTER(0);
LEAVE("ns iter %p (%s)", iter, iter_to_string(iter));
+ g_list_free (list);
return TRUE;
}
@@ -731,6 +736,7 @@ gnc_tree_model_commodity_iter_children (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, 0);
iter->user_data3 = GINT_TO_POINTER(0);
LEAVE("cm iter %p (%s)", iter, iter_to_string(iter));
+ g_list_free (list);
return TRUE;
}
@@ -758,7 +764,9 @@ gnc_tree_model_commodity_iter_has_child (GtkTreeModel *tree_model,
name_space = (gnc_commodity_namespace *)iter->user_data2;
list = gnc_commodity_namespace_get_commodity_list(name_space);
LEAVE("%s children", list ? "has" : "no");
- return list != NULL;
+ gboolean rv = (list != NULL);
+ g_list_free (list);
+ return rv;
}
static int
@@ -780,7 +788,9 @@ gnc_tree_model_commodity_iter_n_children (GtkTreeModel *tree_model,
ct = model->commodity_table;
list = gnc_commodity_table_get_namespaces_list(ct);
LEAVE("ns list length %d", g_list_length(list));
- return g_list_length (list);
+ guint rv = g_list_length (list);
+ g_list_free (list);
+ return rv;
}
if (iter->user_data == ITER_IS_NAMESPACE)
@@ -788,7 +798,9 @@ gnc_tree_model_commodity_iter_n_children (GtkTreeModel *tree_model,
name_space = (gnc_commodity_namespace *)iter->user_data2;
list = gnc_commodity_namespace_get_commodity_list(name_space);
LEAVE("cm list length %d", g_list_length(list));
- return g_list_length (list);
+ guint rv = g_list_length (list);
+ g_list_free (list);
+ return rv;
}
LEAVE("0");
@@ -823,6 +835,7 @@ gnc_tree_model_commodity_iter_nth_child (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, n);
iter->user_data3 = GINT_TO_POINTER(n);
LEAVE("ns iter %p (%s)", iter, iter_to_string(iter));
+ g_list_free (list);
return iter->user_data2 != NULL;
}
@@ -836,6 +849,7 @@ gnc_tree_model_commodity_iter_nth_child (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, n);
iter->user_data3 = GINT_TO_POINTER(n);
LEAVE("cm iter %p (%s)", iter, iter_to_string(iter));
+ g_list_free (list);
return iter->user_data2 != NULL;
}
@@ -877,6 +891,7 @@ gnc_tree_model_commodity_iter_parent (GtkTreeModel *tree_model,
iter->user_data2 = name_space;
iter->user_data3 = GINT_TO_POINTER(g_list_index(list, name_space));
LEAVE("ns iter %p (%s)", iter, iter_to_string(iter));
+ g_list_free (list);
return TRUE;
}
@@ -919,6 +934,7 @@ gnc_tree_model_commodity_get_iter_from_commodity (GncTreeModelCommodity *model,
}
n = g_list_index(list, commodity);
+ g_list_free (list);
if (n == -1)
{
LEAVE("not in list");
@@ -996,6 +1012,7 @@ gnc_tree_model_commodity_get_iter_from_namespace (GncTreeModelCommodity *model,
}
n = g_list_index(list, name_space);
+ g_list_free (list);
if (n == -1)
{
LEAVE("");
diff --git a/gnucash/gnome-utils/gnc-tree-model-price.c b/gnucash/gnome-utils/gnc-tree-model-price.c
index 09b980de05..7f882abcd5 100644
--- a/gnucash/gnome-utils/gnc-tree-model-price.c
+++ b/gnucash/gnome-utils/gnc-tree-model-price.c
@@ -491,6 +491,7 @@ gnc_tree_model_price_get_iter (GtkTreeModel *tree_model,
LEAVE("invalid path at namespace");
return FALSE;
}
+ g_list_free (ns_list);
if (depth == 1)
{
@@ -507,6 +508,7 @@ gnc_tree_model_price_get_iter (GtkTreeModel *tree_model,
cm_list = gnc_commodity_namespace_get_commodity_list(name_space);
i = gtk_tree_path_get_indices (path)[1];
commodity = g_list_nth_data (cm_list, i);
+ g_list_free (cm_list);
if (!commodity)
{
LEAVE("invalid path at commodity");
@@ -594,9 +596,11 @@ gnc_tree_model_price_get_path (GtkTreeModel *tree_model,
gtk_tree_path_append_index (path, g_list_index (ns_list, name_space));
gtk_tree_path_append_index (path, GPOINTER_TO_INT(iter->user_data3));
debug_path(LEAVE, path);
+ g_list_free (ns_list);
return path;
}
+ g_list_free (ns_list);
/* Create a path to the price. */
commodity = gnc_price_get_commodity((GNCPrice*)iter->user_data2);
name_space = gnc_commodity_get_namespace_ds(commodity);
@@ -606,6 +610,7 @@ gnc_tree_model_price_get_path (GtkTreeModel *tree_model,
gtk_tree_path_append_index (path, g_list_index (cm_list, commodity));
gtk_tree_path_append_index (path, GPOINTER_TO_INT(iter->user_data3));
debug_path(LEAVE, path);
+ g_list_free (cm_list);
return path;
}
@@ -759,6 +764,7 @@ gnc_tree_model_price_iter_next (GtkTreeModel *tree_model,
list = gnc_commodity_table_get_namespaces_list(ct);
n = GPOINTER_TO_INT(iter->user_data3) + 1;
iter->user_data2 = g_list_nth_data(list, n);
+ g_list_free (list);
if (iter->user_data2 == NULL)
{
LEAVE("no next iter");
@@ -774,6 +780,7 @@ gnc_tree_model_price_iter_next (GtkTreeModel *tree_model,
list = gnc_commodity_namespace_get_commodity_list(name_space);
n = GPOINTER_TO_INT(iter->user_data3) + 1;
iter->user_data2 = g_list_nth_data(list, n);
+ g_list_free (list);
if (iter->user_data2 == NULL)
{
LEAVE("no next iter");
@@ -836,6 +843,7 @@ gnc_tree_model_price_iter_children (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, 0);
iter->user_data3 = GINT_TO_POINTER(0);
LEAVE("ns iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return TRUE;
}
@@ -854,6 +862,7 @@ gnc_tree_model_price_iter_children (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, 0);
iter->user_data3 = GINT_TO_POINTER(0);
LEAVE("cm iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return TRUE;
}
@@ -906,7 +915,9 @@ gnc_tree_model_price_iter_has_child (GtkTreeModel *tree_model,
name_space = (gnc_commodity_namespace *)iter->user_data2;
list = gnc_commodity_namespace_get_commodity_list(name_space);
LEAVE("%s children", list ? "has" : "no");
- return list != NULL;
+ gboolean rv = (list != NULL);
+ g_list_free (list);
+ return rv;
}
if (iter->user_data == ITER_IS_COMMODITY)
@@ -943,7 +954,9 @@ gnc_tree_model_price_iter_n_children (GtkTreeModel *tree_model,
ct = qof_book_get_data (model->book, GNC_COMMODITY_TABLE);
list = gnc_commodity_table_get_namespaces_list(ct);
LEAVE("ns list length %d", g_list_length(list));
- return g_list_length (list);
+ guint rv = g_list_length (list);
+ g_list_free (list);
+ return rv;
}
if (iter->user_data == ITER_IS_NAMESPACE)
@@ -951,7 +964,9 @@ gnc_tree_model_price_iter_n_children (GtkTreeModel *tree_model,
name_space = (gnc_commodity_namespace *)iter->user_data2;
list = gnc_commodity_namespace_get_commodity_list(name_space);
LEAVE("cm list length %d", g_list_length(list));
- return g_list_length (list);
+ guint rv = g_list_length (list);
+ g_list_free (list);
+ return rv;
}
if (iter->user_data == ITER_IS_COMMODITY)
@@ -995,6 +1010,7 @@ gnc_tree_model_price_iter_nth_child (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, n);
iter->user_data3 = GINT_TO_POINTER(n);
LEAVE("ns iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return iter->user_data2 != NULL;
}
@@ -1008,6 +1024,7 @@ gnc_tree_model_price_iter_nth_child (GtkTreeModel *tree_model,
iter->user_data2 = g_list_nth_data(list, n);
iter->user_data3 = GINT_TO_POINTER(n);
LEAVE("cm iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return iter->user_data2 != NULL;
}
@@ -1064,6 +1081,7 @@ gnc_tree_model_price_iter_parent (GtkTreeModel *tree_model,
iter->user_data2 = name_space;
iter->user_data3 = GINT_TO_POINTER(g_list_index(list, name_space));
LEAVE("ns iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return TRUE;
}
@@ -1076,6 +1094,7 @@ gnc_tree_model_price_iter_parent (GtkTreeModel *tree_model,
iter->user_data2 = commodity;
iter->user_data3 = GINT_TO_POINTER(g_list_index(list, commodity));
LEAVE("cm iter %p (%s)", iter, iter_to_string(model, iter));
+ g_list_free (list);
return TRUE;
}
@@ -1203,6 +1222,7 @@ gnc_tree_model_price_get_iter_from_commodity (GncTreeModelPrice *model,
}
n = g_list_index(list, commodity);
+ g_list_free (list);
if (n == -1)
{
LEAVE("commodity not in list");
@@ -1245,6 +1265,7 @@ gnc_tree_model_price_get_iter_from_namespace (GncTreeModelPrice *model,
}
n = g_list_index(list, name_space);
+ g_list_free (list);
if (n == -1)
{
LEAVE("namespace not found");
diff --git a/gnucash/gnome/dialog-commodities.c b/gnucash/gnome/dialog-commodities.c
index e490b74f65..ec0437d09f 100644
--- a/gnucash/gnome/dialog-commodities.c
+++ b/gnucash/gnome/dialog-commodities.c
@@ -306,7 +306,9 @@ gnc_commodities_dialog_filter_ns_func (gnc_commodity_namespace *name_space,
/* Show any other namespace that has commodities */
list = gnc_commodity_namespace_get_commodity_list(name_space);
- return (list != NULL);
+ gboolean rv = (list != NULL);
+ g_list_free (list);
+ return rv;
}
static gboolean
diff --git a/gnucash/gnome/dialog-price-edit-db.cpp b/gnucash/gnome/dialog-price-edit-db.cpp
index 6f55b3db6e..c129a0f8ab 100644
--- a/gnucash/gnome/dialog-price-edit-db.cpp
+++ b/gnucash/gnome/dialog-price-edit-db.cpp
@@ -635,15 +635,17 @@ gnc_price_dialog_filter_ns_func (gnc_commodity_namespace *name_space,
/* See if this namespace has commodities */
auto cm_list = gnc_commodity_namespace_get_commodity_list (name_space);
- for (auto item = cm_list; item; item = g_list_next (item))
+ auto rv = false;
+ for (auto item = cm_list; !rv && item; item = g_list_next (item))
{
/* For each commodity, see if there are prices */
auto comm = static_cast<gnc_commodity *> (item->data);
if (gnc_pricedb_has_prices (pdb_dialog->price_db, comm, nullptr))
- return TRUE;
+ rv = true;
}
- return FALSE;
+ g_list_free (cm_list);
+ return rv;
}
diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp
index 1438050c80..50d4eaac7e 100644
--- a/libgnucash/app-utils/gnc-quotes.cpp
+++ b/libgnucash/app-utils/gnc-quotes.cpp
@@ -1008,6 +1008,7 @@ gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
{
auto cm_list = gnc_commodity_namespace_get_commodity_list (ns);
g_list_foreach (cm_list, &get_quotables_helper1, (gpointer) &l);
+ g_list_free (cm_list);
}
}
}
diff --git a/libgnucash/engine/gnc-commodity.cpp b/libgnucash/engine/gnc-commodity.cpp
index 26ef9af7f6..caaeea8c0a 100644
--- a/libgnucash/engine/gnc-commodity.cpp
+++ b/libgnucash/engine/gnc-commodity.cpp
@@ -1565,7 +1565,7 @@ gnc_commodity_namespace_get_commodity_list(const gnc_commodity_namespace *name_s
if (!name_space)
return nullptr;
- return name_space->cm_list;
+ return g_list_copy (name_space->cm_list);
}
gboolean
@@ -1950,7 +1950,7 @@ gnc_commodity_table_get_namespaces_list(const gnc_commodity_table * table)
if (!table)
return nullptr;
- return table->ns_list;
+ return g_list_copy (table->ns_list);
}
/* Because gnc_commodity_table_add_namespace maps GNC_COMMODITY_NS_ISO to
diff --git a/libgnucash/engine/gnc-commodity.h b/libgnucash/engine/gnc-commodity.h
index 9fde3220da..6bef47643e 100644
--- a/libgnucash/engine/gnc-commodity.h
+++ b/libgnucash/engine/gnc-commodity.h
@@ -814,7 +814,7 @@ const char * gnc_commodity_namespace_get_gui_name (const gnc_commodity_namespace
* @return A pointer to the list of structures. NULL if an invalid
* argument was supplied.
*
- * @note This list is owned by the engine. The caller must not free the list. */
+ * @note This list is owned by the caller who must free the list. */
GList * gnc_commodity_namespace_get_commodity_list(const gnc_commodity_namespace * ns);
@@ -844,7 +844,7 @@ GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table * t);
* @return A pointer to the list of structures. NULL if an invalid
* argument was supplied.
*
- * @note This list is owned by the engine. The caller must not free the list. */
+ * @note This list is owned by the caller who must free the list. */
GList * gnc_commodity_table_get_namespaces_list(const gnc_commodity_table * t);
/** This function adds a new string to the list of commodity namespaces.
Summary of changes:
gnucash/gnome-utils/gnc-tree-model-commodity.c | 23 +++++++++++++++++++---
gnucash/gnome-utils/gnc-tree-model-price.c | 27 +++++++++++++++++++++++---
gnucash/gnome/dialog-commodities.c | 4 +++-
gnucash/gnome/dialog-price-edit-db.cpp | 8 +++++---
libgnucash/app-utils/gnc-quotes.cpp | 1 +
libgnucash/engine/gnc-commodity.cpp | 4 ++--
libgnucash/engine/gnc-commodity.h | 4 ++--
7 files changed, 57 insertions(+), 14 deletions(-)
More information about the gnucash-changes
mailing list