From f26014a04e26278827fa9f67f487bd7936e93eee Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sat, 11 Dec 2021 15:11:26 -0800 Subject: [PATCH] c++options: QofInstanceValue: Protect against crashes when m_value is nullptr. --- libgnucash/app-utils/gnc-option-impl.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libgnucash/app-utils/gnc-option-impl.cpp b/libgnucash/app-utils/gnc-option-impl.cpp index e36d9b1a5a..5131aef8cc 100644 --- a/libgnucash/app-utils/gnc-option-impl.cpp +++ b/libgnucash/app-utils/gnc-option-impl.cpp @@ -435,8 +435,9 @@ GncOptionValue::reset_default_value() template std::string GncOptionValue::serialize() const noexcept { + static const std::string no_value{"No Value"}; if constexpr(std::is_same_v) - return qof_instance_to_string(m_value); + return m_value ? qof_instance_to_string(m_value) : no_value; else if constexpr(is_same_decayed_v) return m_value; else if constexpr(is_same_decayed_v) @@ -500,8 +501,9 @@ GncOptionValue::reset_default_value() template std::string GncOptionValidatedValue::serialize() const noexcept { + static const std::string no_value{"No Value"}; if constexpr(std::is_same_v) - return qof_instance_to_string(m_value); + return m_value ? qof_instance_to_string(m_value) : no_value; else if constexpr(is_same_decayed_v) return m_value; else if constexpr(is_same_decayed_v) @@ -535,8 +537,11 @@ GncOptionValidatedValue::deserialize(const std::string& str) noexcept std::string GncOptionAccountListValue::serialize() const noexcept { + static const std::string no_value{"No Value"}; std::string retval; bool first = true; + if (m_value.empty()) + return no_value; for (auto val : m_value) { if (!first) @@ -571,7 +576,8 @@ GncOptionAccountListValue::deserialize(const std::string& str) noexcept std::string GncOptionAccountSelValue::serialize() const noexcept { - return qof_instance_to_string(QOF_INSTANCE(m_value)); + static const std::string no_value{"No Value"}; + return m_value ?qof_instance_to_string(QOF_INSTANCE(m_value)) : no_value; } bool @@ -584,8 +590,11 @@ GncOptionAccountSelValue::deserialize(const std::string& str) noexcept std::string GncOptionMultichoiceValue::serialize() const noexcept { + static const std::string no_value{"No Value"}; std::string retval; bool first = true; + if (m_value.empty()) + return no_value; for (auto index : m_value) { if (!first)