gnucash stable: [account.cpp] gnc_account_child_index non-child acct should return -1

Christopher Lam clam at code.gnucash.org
Sun Jan 4 18:20:48 EST 2026


Updated	 via  https://github.com/Gnucash/gnucash/commit/c3e4f374 (commit)
	from  https://github.com/Gnucash/gnucash/commit/65d57388 (commit)



commit c3e4f374a91edc1b3484c283dd1dfd34de7fc75e
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Mon Jan 5 07:17:04 2026 +0800

    [account.cpp] gnc_account_child_index non-child acct should return -1
    
    bugfix from 6cac9d0ebb

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index da3ed89542..ed795cc502 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -2952,7 +2952,8 @@ gnc_account_child_index (const Account *parent, const Account *child)
     g_return_val_if_fail(GNC_IS_ACCOUNT(parent), -1);
     g_return_val_if_fail(GNC_IS_ACCOUNT(child), -1);
     auto& children = GET_PRIVATE(parent)->children;
-    return std::distance (children.begin(), std::find (children.begin(), children.end(), child));
+    auto find_it = std::find (children.begin(), children.end(), child);
+    return find_it == children.end() ? -1 : std::distance (children.begin(), find_it);
 }
 
 Account *
diff --git a/libgnucash/engine/test/utest-Account.cpp b/libgnucash/engine/test/utest-Account.cpp
index 2415914168..da8d240b63 100644
--- a/libgnucash/engine/test/utest-Account.cpp
+++ b/libgnucash/engine/test/utest-Account.cpp
@@ -1854,6 +1854,21 @@ test_gnc_account_append_remove_child (Fixture *fixture, gconstpointer pData)
  * gnc_account_child_index
  * gnc_account_nth_child
  */
+
+static void
+test_gnc_account_child_index (Fixture *fixture, gconstpointer pData)
+{
+    auto root{gnc_account_get_root (fixture->acct)};
+    g_assert_cmpint (gnc_account_child_index (root, fixture->acct), == , -1);
+
+    auto book{gnc_account_get_book (fixture->acct)};
+    auto new_acct{xaccMallocAccount(book)};
+    g_assert_cmpint (gnc_account_child_index (root, new_acct), == , -1);
+
+    gnc_account_append_child (root, new_acct);
+    g_assert_cmpint (gnc_account_child_index (root, new_acct), == , 2);
+}
+
 /* gnc_account_n_descendants
 gint
 gnc_account_n_descendants (const Account *account)// C: 12 in 6 */
@@ -2860,6 +2875,7 @@ test_suite_account (void)
     GNC_TEST_ADD_FUNC (suitename, "xaccAccountOrder", test_xaccAccountOrder );
     GNC_TEST_ADD (suitename, "qofAccountSetParent", Fixture, &some_data, setup, test_qofAccountSetParent,  teardown );
     GNC_TEST_ADD (suitename, "gnc account append/remove child", Fixture, NULL, setup, test_gnc_account_append_remove_child,  teardown );
+    GNC_TEST_ADD (suitename, "test_gnc_account_child_index", Fixture, &some_data, setup, test_gnc_account_child_index,  teardown );
     GNC_TEST_ADD (suitename, "gnc account n descendants", Fixture, &some_data, setup, test_gnc_account_n_descendants,  teardown );
     GNC_TEST_ADD (suitename, "gnc account get current depth", Fixture, &some_data, setup, test_gnc_account_get_current_depth,  teardown );
     GNC_TEST_ADD (suitename, "gnc account get tree depth", Fixture, &complex, setup, test_gnc_account_get_tree_depth,  teardown );



Summary of changes:
 libgnucash/engine/Account.cpp            |  3 ++-
 libgnucash/engine/test/utest-Account.cpp | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)



More information about the gnucash-changes mailing list