Skip to content

Commit

Permalink
[gnc-commodity.h] callers must free g_list
Browse files Browse the repository at this point in the history
callers to gnc_commodity_namespace_get_commodity_list and
gnc_commodity_table_get_namespaces_list must free the GList.
  • Loading branch information
christopherlam committed Aug 27, 2024
1 parent 0fc6e4f commit a99491d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
23 changes: 20 additions & 3 deletions gnucash/gnome-utils/gnc-tree-model-commodity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -780,15 +788,19 @@ 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)
{
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");
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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("");
Expand Down
27 changes: 24 additions & 3 deletions gnucash/gnome-utils/gnc-tree-model-price.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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");
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -943,15 +954,19 @@ 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)
{
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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 3 additions & 1 deletion gnucash/gnome/dialog-commodities.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions gnucash/gnome/dialog-price-edit-db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
1 change: 1 addition & 0 deletions libgnucash/app-utils/gnc-quotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions libgnucash/engine/gnc-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libgnucash/engine/gnc-commodity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a99491d

Please sign in to comment.