From: Ralf Habacker Date: Wed, 8 Dec 2021 15:02:08 +0000 (+0100) Subject: Fix memory leaks in Windows variant of _dbus_directory_open() X-Git-Tag: dbus-1.13.20~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b804ed77d336770354939e24b5b09ed95d1b4ba;p=thirdparty%2Fdbus.git Fix memory leaks in Windows variant of _dbus_directory_open() 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 --- diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 4c31bdc3b..529a099e6 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -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; } /** diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c index 4e23b4947..c572fcd0b 100644 --- a/dbus/dbus-sysdeps-util-win.c +++ b/dbus/dbus-sysdeps-util-win.c @@ -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;