gnucash stable: Multiple changes pushed

Christopher Lam clam at code.gnucash.org
Tue Apr 4 20:26:26 EDT 2023


Updated	 via  https://github.com/Gnucash/gnucash/commit/5236f337 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/56b75c36 (commit)
	from  https://github.com/Gnucash/gnucash/commit/bc1f5b80 (commit)



commit 5236f33709134da20b3847acc9d953c0a3e8ac65
Merge: bc1f5b80c1 56b75c3691
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Wed Apr 5 08:25:58 2023 +0800

    Merge branch 'plug-scm-to-utf8-string-leaks' into stable #1599


commit 56b75c369164c1777063198965b62cd17e548c57
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Tue Apr 4 23:20:44 2023 +0800

    std::string cannot free() scm_to_utf8_string automatically

diff --git a/bindings/guile/gnc-optiondb.i b/bindings/guile/gnc-optiondb.i
index 6ee883438b..d8ec8dfc0f 100644
--- a/bindings/guile/gnc-optiondb.i
+++ b/bindings/guile/gnc-optiondb.i
@@ -413,23 +413,25 @@ scm_to_value<gnc_commodity*>(SCM new_value)
     if (scm_is_list(new_value))
     {
         auto len{scm_to_uint(scm_length(new_value))};
-        std::string mnemonic{scm_to_utf8_string(scm_list_ref(new_value,
-                                                         scm_from_uint(0)))};
-        std::string name_space{"CURRENCY"};
-        if (len > 1)
-           name_space = scm_to_utf8_string(scm_list_ref(new_value,
-                                                        scm_from_uint(1)));
+        auto mnemonic{scm_to_utf8_string(scm_list_ref(new_value, scm_from_uint(0)))};
+        auto name_space{(len > 1) ? scm_to_utf8_string(scm_list_ref(new_value,
+                                                                    scm_from_uint(1)))
+                : strdup ("CURRENCY")};
         auto book{get_current_book()};
         auto table = gnc_commodity_table_get_table(book);
-        return gnc_commodity_table_lookup(table, name_space.c_str(),
-                                          mnemonic.c_str());
+        auto rv = gnc_commodity_table_lookup (table, name_space, mnemonic);
+        free (name_space);
+        free (mnemonic);
+        return rv;
     }
     if (scm_is_string(new_value))
     {
         auto book{get_current_book()};
         auto table = gnc_commodity_table_get_table(book);
-        std::string mnemonic{scm_to_utf8_string(new_value)};
-        return gnc_commodity_table_lookup(table, "CURRENCY", mnemonic.c_str());
+        auto mnemonic{scm_to_utf8_string(new_value)};
+        auto rv = gnc_commodity_table_lookup(table, "CURRENCY", mnemonic);
+        free (mnemonic);
+        return rv;
     }
     return nullptr;
 }
@@ -483,6 +485,7 @@ scm_to_value<GncOptionAccountList>(SCM new_value)
             GncGUID guid;
             string_to_guid(guid_str, &guid);
             retval.push_back(guid);
+            free (guid_str);
         }
         else
         {
@@ -635,9 +638,11 @@ gnc_option_test_book_destroy(QofBook* book)
         }
         else
             throw std::invalid_argument("Unsupported key type in multichoice option.");
-        std::string key{scm_to_utf8_string(keyval)};
-        std::string name{scm_to_utf8_string(SCM_SIMPLE_VECTOR_REF(vec, 1))};
-        choices.push_back({std::move(key), std::move(name), keytype});
+        auto key{scm_to_utf8_string(keyval)};
+        auto name{scm_to_utf8_string(SCM_SIMPLE_VECTOR_REF(vec, 1))};
+        choices.push_back({key, name, keytype});
+        free (name);
+        free (key);
     }
     $1 = &choices;
  }
@@ -928,12 +933,15 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
             if (scm_is_symbol(scm_car(date)))
             {
                 auto car{scm_to_utf8_string(scm_symbol_to_string(scm_car(date)))};
+                bool rv = false;
                 if (strcmp(car, "relative") == 0)
-                    return false;
-                if (strcmp(car, "absolute") == 0)
-                    return true;
-
-                assert(false);
+                    rv = false;
+                else if (strcmp(car, "absolute") == 0)
+                    rv = true;
+                else
+                    assert(false);
+                free (car);
+                return rv;
             }
             else
             {
@@ -985,7 +993,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
                         const GncOptionMultichoiceValue& option)
     {
         static const auto uint16_t_max = std::numeric_limits<uint16_t>::max();
-        auto scm_to_str = [](auto item)->const char* {
+        auto scm_to_str = [](auto item)->char* {
                 static const char* empty{""};
                 if (scm_is_integer(item))
                     item = scm_number_to_string(item, scm_from_uint(10u));
@@ -993,7 +1001,7 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
                     item = scm_symbol_to_string(item);
                 if (scm_is_string(item))
                     return scm_to_utf8_string(item);
-                return empty;
+                return strdup(empty);
             };
         GncMultichoiceOptionIndexVec vec;
         auto choice_is_list{option.get_ui_type() == GncOptionUIType::LIST}; 
@@ -1005,14 +1013,18 @@ wrap_unique_ptr(GncOptionDBPtr, GncOptionDB);
             for (std::size_t i = 0; i < len; ++i)
             {
                 auto item{scm_list_ref(new_value, scm_from_size_t(i))};
-                auto index{option.permissible_value_index(scm_to_str(item))};
+                auto item_str{scm_to_str(item)};
+                auto index{option.permissible_value_index(item_str)};
+                free (item_str);
                 if (index < uint16_t_max)
                     vec.push_back(index);
             }
         }
         else
         {
-            auto index{option.permissible_value_index(scm_to_str(new_value))};
+            auto newval_str{scm_to_str(new_value)};
+            auto index{option.permissible_value_index(newval_str)};
+            free (newval_str);
             if (index < uint16_t_max)
                 vec.push_back(index);
         }
@@ -1396,6 +1408,7 @@ inline SCM return_scm_value(ValueType value)
                             {
                                  auto strval{scm_to_utf8_string(new_value)};
                                  option.deserialize(strval);
+                                 free (strval);
                                  return;
                             }
                             option.set_value(scm_to_value<gnc_commodity*>(new_value));
@@ -1405,13 +1418,18 @@ inline SCM return_scm_value(ValueType value)
                         if (len > 1)
                         {
                             auto revlist{scm_reverse(new_value)};
-                            std::string name_space{scm_to_utf8_string(scm_cadr(revlist))};
-                            std::string mnemonic{scm_to_utf8_string(scm_car(revlist))};
-                            option.deserialize(name_space + ":" + mnemonic);
+                            auto name_space{scm_to_utf8_string(scm_cadr(revlist))};
+                            auto mnemonic{scm_to_utf8_string(scm_car(revlist))};
+                            option.deserialize(std::string (name_space) + ":" +
+                                               std::string (mnemonic));
+                            free (mnemonic);
+                            free (name_space);
                         }
                         else
                         {
-                            option.deserialize(scm_to_utf8_string(scm_car(new_value)));
+                            auto newval_str{scm_to_utf8_string(scm_car(new_value))};
+                            option.deserialize(newval_str);
+                            free (newval_str);
                         }
                         return;
                     }
@@ -1422,6 +1440,7 @@ inline SCM return_scm_value(ValueType value)
                             auto strval{scm_to_utf8_string(new_value)};
                             auto val{qof_instance_from_string(strval, option.get_ui_type())};
                             option.set_value(val);
+                            free (strval);
                         }
                         else
                         {
@@ -1439,6 +1458,7 @@ inline SCM return_scm_value(ValueType value)
                             auto strval{scm_to_utf8_string(scm_cdr(new_value))};
                             owner.owner.undefined = qof_instance_from_string(strval, option.get_ui_type());
                             option.set_value(&owner);
+                            free (strval);
                         }
                         else
                         {
@@ -1472,6 +1492,7 @@ inline SCM return_scm_value(ValueType value)
                             string_to_guid(strval, &guid);
                             auto book{get_current_book()};
                             option.set_value(xaccAccountLookup(&guid, book));
+                            free (strval);
                         }
                         else
                         {
@@ -1536,6 +1557,7 @@ inline SCM return_scm_value(ValueType value)
                             auto strval{scm_to_utf8_string(new_value)};
                             auto val{qof_instance_from_string(strval, option.get_ui_type())};
                             option.set_default_value(val);
+                            free (strval);
                         }
                         else
                         {
@@ -1553,6 +1575,7 @@ inline SCM return_scm_value(ValueType value)
                             auto strval{scm_to_utf8_string(scm_cdr(new_value))};
                             owner.owner.undefined = qof_instance_from_string(strval, option.get_ui_type());
                             option.set_default_value(&owner);
+                            free (strval);
                         }
                         else
                         {
@@ -1585,6 +1608,7 @@ inline SCM return_scm_value(ValueType value)
                             string_to_guid(strval, &guid);
                             auto book{get_current_book()};
                             option.set_default_value(xaccAccountLookup(&guid, book));
+                            free (strval);
                         }
                         else
                         {
diff --git a/gnucash/gnome/dialog-custom-report.c b/gnucash/gnome/dialog-custom-report.c
index 2aa4af39ba..593c12fd28 100644
--- a/gnucash/gnome/dialog-custom-report.c
+++ b/gnucash/gnome/dialog-custom-report.c
@@ -472,11 +472,12 @@ custom_report_name_edited_cb(GtkCellRendererText *renderer, gchar *path, gchar *
     if (scm_is_true (scm_call_2 (unique_name_func, guid, new_name_scm)))
     {
         gchar *default_guid = gnc_get_default_invoice_print_report ();
+        gchar *guid_string = scm_to_utf8_string (guid);
 
         custom_report_edit_report_name (guid, crd, new_text);
 
         // check to see if default report name has been changed
-        if (g_strcmp0 (default_guid, scm_to_utf8_string (guid)) == 0)
+        if (g_strcmp0 (default_guid, guid_string) == 0)
         {
             QofBook *book = gnc_get_current_book ();
             gchar *default_name = qof_book_get_default_invoice_report_name (book);
@@ -485,6 +486,7 @@ custom_report_name_edited_cb(GtkCellRendererText *renderer, gchar *path, gchar *
                 qof_book_set_default_invoice_report (book, default_guid, new_text);
             g_free (default_name);
         }
+        g_free (guid_string);
         g_free (default_guid);
     }
     else
diff --git a/gnucash/gnome/dialog-report-column-view.cpp b/gnucash/gnome/dialog-report-column-view.cpp
index 6cb31baeac..10f772b8ba 100644
--- a/gnucash/gnome/dialog-report-column-view.cpp
+++ b/gnucash/gnome/dialog-report-column-view.cpp
@@ -115,7 +115,11 @@ get_available_reports ()
     StrVec sv;
     auto scm_list{scm_call_0(scm_c_eval_string("gnc:all-report-template-guids"))};
     for (auto next{scm_list}; !scm_is_null(next); next = scm_cdr(next))
-        sv.emplace_back(scm_to_utf8_string(scm_car(next)));
+    {
+        auto guid{scm_to_utf8_string(scm_car(next))};
+        sv.emplace_back(guid);
+        g_free (guid);
+    }
     return sv;
 }
 



Summary of changes:
 bindings/guile/gnc-optiondb.i               | 76 +++++++++++++++++++----------
 gnucash/gnome/dialog-custom-report.c        |  4 +-
 gnucash/gnome/dialog-report-column-view.cpp |  6 ++-
 3 files changed, 58 insertions(+), 28 deletions(-)



More information about the gnucash-changes mailing list