[glib] gsettings: Fix memory leak on error handling path for g_settings_set()



commit 08b6794ec27782e5c1c431c602dd9ae97d813d17
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Feb 27 10:04:39 2017 +0000

    gsettings: Fix memory leak on error handling path for g_settings_set()
    
    On the warning/critical error handling paths for g_settings_set(), the
    GVariant value was not ref-sunk, and the schema key was leaked. This
    won’t affect code in production (unless it’s seriously buggy), but
    eliminates some leaks from the error testing paths in the GSettings
    tests.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779265

 gio/gsettings.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
---
diff --git a/gio/gsettings.c b/gio/gsettings.c
index 2b974e9..b959633 100644
--- a/gio/gsettings.c
+++ b/gio/gsettings.c
@@ -1571,6 +1571,7 @@ g_settings_set_value (GSettings   *settings,
   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
   g_return_val_if_fail (key != NULL, FALSE);
 
+  g_variant_ref_sink (value);
   g_settings_schema_key_init (&skey, settings->priv->schema, key);
 
   if (!g_settings_schema_key_type_check (&skey, value))
@@ -1580,22 +1581,23 @@ g_settings_set_value (GSettings   *settings,
                   g_settings_schema_get_id (settings->priv->schema),
                   g_variant_type_peek_string (skey.type),
                   g_variant_get_type_string (value));
-
-        return FALSE;
-      }
-
-  if (!g_settings_schema_key_range_check (&skey, value))
+      success = FALSE;
+    }
+  else if (!g_settings_schema_key_range_check (&skey, value))
     {
       g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
                  "is outside of valid range",
                  key,
                  g_settings_schema_get_id (settings->priv->schema));
-
-        return FALSE;
+      success = FALSE;
+    }
+  else
+    {
+      success = g_settings_write_to_backend (settings, &skey, value);
     }
 
-  success = g_settings_write_to_backend (settings, &skey, value);
   g_settings_schema_key_clear (&skey);
+  g_variant_unref (value);
 
   return success;
 }


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