]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Fix memory leaks in Windows variant of _dbus_directory_open()
authorRalf Habacker <ralf.habacker@freenet.de>
Wed, 8 Dec 2021 15:02:08 +0000 (16:02 +0100)
committerRalf Habacker <ralf.habacker@freenet.de>
Thu, 9 Dec 2021 11:53:18 +0000 (12:53 +0100)
If the memory is not sufficient, the created DBusString instance must be
released. This belongs to the mentioned function and
_dbus_string_init_from_string().

Fixes #360

dbus/dbus-string.c
dbus/dbus-sysdeps-util-win.c

index 4c31bdc3b17d6c6ff2f70f6849a13299dc984c28..529a099e64b7ce7433d7e989df2eeacb91562ad1 100644 (file)
@@ -241,8 +241,10 @@ _dbus_string_init_const_len (DBusString *str,
 }
 
 /**
- * Initializes a string from another string. The
- * string must eventually be freed with _dbus_string_free().
+ * Initializes a string from another string
+ *
+ * The string must be freed with _dbus_string_free() in case of success.
+ * In case of error the string is freed by this function itself.
  *
  * @param str memory to hold the string
  * @param from instance from which the string is initialized
@@ -252,9 +254,14 @@ dbus_bool_t
 _dbus_string_init_from_string(DBusString       *str,
                               const DBusString *from)
 {
- if (!_dbus_string_init (str))
-     return FALSE;
- return _dbus_string_append (str, _dbus_string_get_const_data (from));
+  if (!_dbus_string_init (str))
+    return FALSE;
+  if (!_dbus_string_append (str, _dbus_string_get_const_data (from)))
+    {
+      _dbus_string_free (str);
+      return FALSE;
+    }
+  return TRUE;
 }
 
 /**
index 4e23b49477ea0432ef08201c02fd99b7bdfc0b67..c572fcd0bb40b3fc99da27f1424051d07d7723e8 100644 (file)
@@ -426,6 +426,7 @@ _dbus_directory_open (const DBusString *filename,
     {
       if (!_dbus_string_append (&filespec, "*"))
         {
+          _dbus_string_free (&filespec);
           dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
                           "Could not append filename wildcard");
           return NULL;
@@ -435,6 +436,7 @@ _dbus_directory_open (const DBusString *filename,
     {
       if (!_dbus_string_append (&filespec, "\\*"))
         {
+          _dbus_string_free (&filespec);
           dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
                           "Could not append filename wildcard 2");
           return NULL;