From 45a01b0f67a3fca1781be2df91153be29410e05f Mon Sep 17 00:00:00 2001 From: John Ralls Date: Thu, 18 Jun 2015 13:02:00 -0700 Subject: [PATCH] Change the KVP string storage type from char* to const char*. Because we don't want to be able to get a pointer to the KvpValue and change its contents without using KvpValue::set(). --- src/backend/xml/sixtp-dom-generators.cpp | 2 +- src/core-utils/gnc-features.c | 2 +- src/libqof/qof/kvp-value.cpp | 10 +++++----- src/libqof/qof/kvp-value.hpp | 2 +- src/libqof/qof/kvp_frame.cpp | 4 ++-- src/libqof/qof/kvp_frame.h | 2 +- src/libqof/qof/qofbook.cpp | 5 +++-- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/backend/xml/sixtp-dom-generators.cpp b/src/backend/xml/sixtp-dom-generators.cpp index b27ec2282d..11ef34ae27 100644 --- a/src/backend/xml/sixtp-dom-generators.cpp +++ b/src/backend/xml/sixtp-dom-generators.cpp @@ -262,7 +262,7 @@ add_kvp_value_node(xmlNodePtr node, const gchar *tag, KvpValue* val) { case KVP_TYPE_STRING: { - auto newstr = g_strdup(val->get()); + auto newstr = g_strdup(val->get()); val_node = xmlNewTextChild(node, NULL, BAD_CAST tag, checked_char_cast (newstr)); g_free (newstr); diff --git a/src/core-utils/gnc-features.c b/src/core-utils/gnc-features.c index 5b341c9f39..8e733332bc 100644 --- a/src/core-utils/gnc-features.c +++ b/src/core-utils/gnc-features.c @@ -124,7 +124,7 @@ gchar *gnc_features_test_unknown (QofBook *book) g_list_free(features_list); return msg; } - + g_hash_table_unref (features_used); return NULL; } diff --git a/src/libqof/qof/kvp-value.cpp b/src/libqof/qof/kvp-value.cpp index 499b575ad8..d24c6be06c 100644 --- a/src/libqof/qof/kvp-value.cpp +++ b/src/libqof/qof/kvp-value.cpp @@ -80,7 +80,7 @@ KvpValueImpl::get_type() const noexcept return KvpValueType::KVP_TYPE_DOUBLE; else if (datastore.type() == typeid(gnc_numeric)) return KvpValueType::KVP_TYPE_NUMERIC; - else if (datastore.type() == typeid(gchar *)) + else if (datastore.type() == typeid(const gchar *)) return KvpValueType::KVP_TYPE_STRING; else if (datastore.type() == typeid(GncGUID *)) return KvpValueType::KVP_TYPE_GUID; @@ -202,7 +202,7 @@ struct to_string_visitor : boost::static_visitor output << ")"; } - void operator()(char * val) + void operator()(const char * val) { output << "KVP_VALUE_STRING(" << val << ")"; } @@ -243,7 +243,7 @@ struct compare_visitor : boost::static_visitor return 0; } }; -template <> int compare_visitor::operator()(char * const & one, char * const & two) const +template <> int compare_visitor::operator()(const char * const & one, const char * const & two) const { return strcmp(one, two); } @@ -337,8 +337,8 @@ KvpValueImpl::~KvpValueImpl() noexcept void KvpValueImpl::duplicate(const KvpValueImpl& other) noexcept { - if (other.datastore.type() == typeid(gchar *)) - this->datastore = g_strdup(other.get()); + if (other.datastore.type() == typeid(const gchar *)) + this->datastore = g_strdup(other.get()); else if (other.datastore.type() == typeid(GncGUID*)) this->datastore = guid_copy(other.get()); else if (other.datastore.type() == typeid(GList*)) diff --git a/src/libqof/qof/kvp-value.hpp b/src/libqof/qof/kvp-value.hpp index ebd8cb6202..2ab80d3370 100644 --- a/src/libqof/qof/kvp-value.hpp +++ b/src/libqof/qof/kvp-value.hpp @@ -108,7 +108,7 @@ struct KvpValueImpl int64_t, double, gnc_numeric, - char *, + const char*, GncGUID *, Timespec, GList *, diff --git a/src/libqof/qof/kvp_frame.cpp b/src/libqof/qof/kvp_frame.cpp index 3dd415f32f..b60ff162f5 100644 --- a/src/libqof/qof/kvp_frame.cpp +++ b/src/libqof/qof/kvp_frame.cpp @@ -1050,12 +1050,12 @@ kvp_value_get_numeric(const KvpValue * ovalue) return value->get(); } -char * +const char * kvp_value_get_string(const KvpValue * ovalue) { if (!ovalue) return {}; const KvpValueImpl * value {static_cast(ovalue)}; - return value->get(); + return value->get(); } GncGUID * diff --git a/src/libqof/qof/kvp_frame.h b/src/libqof/qof/kvp_frame.h index 8053539a62..7de7dc412a 100644 --- a/src/libqof/qof/kvp_frame.h +++ b/src/libqof/qof/kvp_frame.h @@ -528,7 +528,7 @@ gnc_numeric kvp_value_get_numeric(const KvpValue * value); /** Value accessor. This one is non-copying -- the caller can modify * the value directly. */ -char * kvp_value_get_string(const KvpValue * value); +const char* kvp_value_get_string(const KvpValue * value); /** Value accessor. This one is non-copying -- the caller can modify * the value directly. */ diff --git a/src/libqof/qof/qofbook.cpp b/src/libqof/qof/qofbook.cpp index ebc769454c..68df3997e2 100644 --- a/src/libqof/qof/qofbook.cpp +++ b/src/libqof/qof/qofbook.cpp @@ -1068,7 +1068,7 @@ static void commit_err (G_GNUC_UNUSED QofInstance *inst, QofBackendError errcode static void add_feature_to_hash (const gchar *key, KvpValue *value, gpointer user_data) { - gchar *descr = kvp_value_get_string (value); + gchar *descr = g_strdup(kvp_value_get_string (value)); g_hash_table_insert (*(GHashTable**)user_data, (gchar*)key, descr); } @@ -1076,7 +1076,8 @@ GHashTable * qof_book_get_features (QofBook *book) { KvpFrame *frame = qof_instance_get_slots (QOF_INSTANCE (book)); - GHashTable *features = g_hash_table_new (g_str_hash, g_str_equal); + GHashTable *features = g_hash_table_new_full (g_str_hash, g_str_equal, + NULL, g_free); frame = kvp_frame_get_frame (frame, GNC_FEATURES); kvp_frame_for_each_slot (frame, &add_feature_to_hash, &features);