[glibmm: 2/3] Use convert_return_gchar_ptr_to_*() in a couple of ustring methods



commit 5d634e3036fd2387933a89c93f7ef9d6cddc7cce
Author: Martin Ejdestig <marejde gmail com>
Date:   Thu Apr 18 07:03:03 2019 +0200

    Use convert_return_gchar_ptr_to_*() in a couple of ustring methods
    
    First attempt at fixing memory leak in make_valid() (see previous commit)
    used make_unique_ptr_gfree() directly because of looking at these methods.
    Better to use the helper function.
    
    This actually fixes undefined behavior for normalize() since
    g_utf8_normalize() is documented to return NULL if string is not a valid
    UTF-8 string. The constructor for std::string, which ustring uses for
    storage, that takes a pointer is documented to have undefined behavior if
    pointer is NULL. The utility function checks for NULL and uses the default
    constructor in that case. (Have not looked at implementation of Glib
    functions, and it may be that all std::string implementations Glibmm is
    used with handles this case, but good to avoid undefined behavior
    regardless, I think.)

 glib/glibmm/ustring.cc | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
---
diff --git a/glib/glibmm/ustring.cc b/glib/glibmm/ustring.cc
index e56b3d9b..d17107a2 100644
--- a/glib/glibmm/ustring.cc
+++ b/glib/glibmm/ustring.cc
@@ -1242,37 +1242,32 @@ ustring::is_ascii() const
 ustring
 ustring::normalize(NormalizeMode mode) const
 {
-  const auto buf = make_unique_ptr_gfree(
+  return convert_return_gchar_ptr_to_ustring(
     g_utf8_normalize(string_.data(), string_.size(), static_cast<GNormalizeMode>(int(mode))));
-  return ustring(buf.get());
 }
 
 ustring
 ustring::uppercase() const
 {
-  const auto buf = make_unique_ptr_gfree(g_utf8_strup(string_.data(), string_.size()));
-  return ustring(buf.get());
+  return convert_return_gchar_ptr_to_ustring(g_utf8_strup(string_.data(), string_.size()));
 }
 
 ustring
 ustring::lowercase() const
 {
-  const auto buf = make_unique_ptr_gfree(g_utf8_strdown(string_.data(), string_.size()));
-  return ustring(buf.get());
+  return convert_return_gchar_ptr_to_ustring(g_utf8_strdown(string_.data(), string_.size()));
 }
 
 ustring
 ustring::casefold() const
 {
-  const auto buf = make_unique_ptr_gfree(g_utf8_casefold(string_.data(), string_.size()));
-  return ustring(buf.get());
+  return convert_return_gchar_ptr_to_ustring(g_utf8_casefold(string_.data(), string_.size()));
 }
 
 std::string
 ustring::collate_key() const
 {
-  const auto buf = make_unique_ptr_gfree(g_utf8_collate_key(string_.data(), string_.size()));
-  return std::string(buf.get());
+  return convert_return_gchar_ptr_to_stdstring(g_utf8_collate_key(string_.data(), string_.size()));
 }
 
 std::string


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]