gnucash stable: Ensure incorrectly coded counter KVP is read correctly.
John Ralls
jralls at code.gnucash.org
Fri May 26 21:27:02 EDT 2023
Updated via https://github.com/Gnucash/gnucash/commit/6111184c (commit)
from https://github.com/Gnucash/gnucash/commit/7d566389 (commit)
commit 6111184c735f98da7b2f5dcf529f5660a16d6bb3
Author: John Ralls <jralls at ceridwen.us>
Date: Fri May 26 21:24:06 2023 -0400
Ensure incorrectly coded counter KVP is read correctly.
Bug 798930 caused some counters to be saved as KVP double. If the
correct int64_t value isn't found when reading, or if a double is
encountered when loading the option dialog, get the double value
and cast it to int64_t.
diff --git a/libgnucash/engine/gnc-optiondb.cpp b/libgnucash/engine/gnc-optiondb.cpp
index 117848fdd2..ba08e05eeb 100644
--- a/libgnucash/engine/gnc-optiondb.cpp
+++ b/libgnucash/engine/gnc-optiondb.cpp
@@ -512,10 +512,25 @@ GncOptionDB::load_from_kvp(QofBook* book) noexcept
auto kvp = qof_book_get_option(book, &list_head);
if (!kvp)
return;
+
+ auto set_double = [&option, kvp, &list_head]() {
+ /*counters might have been set as doubles
+ * because of
+ * https://bugs.gnucash.org/show_bug.cgi?id=798930. They
+ * should be int64_t.
+ */
+ constexpr const char *counters{"counters"};
+ auto value{kvp->get<double>()};
+ if (strcmp(static_cast<char*>(list_head.data), counters) == 0)
+ option.set_value(static_cast<int64_t>(value));
+ else
+ option.set_value(value);
+ };
+
switch (kvp->get_type())
{
case KvpValue::Type::DOUBLE:
- option.set_value(kvp->get<double>());
+ set_double();
break;
case KvpValue::Type::INT64:
option.set_value(kvp->get<int64_t>());
diff --git a/libgnucash/engine/qofbook.cpp b/libgnucash/engine/qofbook.cpp
index a049535dec..13bdb71f5d 100644
--- a/libgnucash/engine/qofbook.cpp
+++ b/libgnucash/engine/qofbook.cpp
@@ -591,8 +591,13 @@ qof_book_get_counter (QofBook *book, const char *counter_name)
value = kvp->get_slot({"counters", counter_name});
if (value)
{
- /* found it */
- return value->get<int64_t>();
+ auto int_value{value->get<int64_t>()};
+ /* Might be a double because of
+ * https://bugs.gnucash.org/show_bug.cgi?id=798930
+ */
+ if (!int_value)
+ int_value = static_cast<int64_t>(value->get<double>());
+ return int_value;
}
else
{
Summary of changes:
libgnucash/engine/gnc-optiondb.cpp | 17 ++++++++++++++++-
libgnucash/engine/qofbook.cpp | 9 +++++++--
2 files changed, 23 insertions(+), 3 deletions(-)
More information about the gnucash-changes
mailing list