[PATCH 4/6] xserver: Memory leak fixes (configAddDevice)

Magnus Vigerlöf Magnus.Vigerlof at home.se
Wed Mar 28 15:09:31 PDT 2007


Memory leak fixes (configAddDevice)

Additional checks for the allocated memory introduced and deallocation
of the option list after it has been used.

--
 config/config.c |   27 ++++++++++++---------------
 1 files changed, 12 insertions(+), 15 deletions(-)

--
diff --git a/config/config.c b/config/config.c
index 02c2f23..4462da6 100644
--- a/config/config.c
+++ b/config/config.c
@@ -110,6 +110,11 @@ configAddDevice(DBusMessage *message, DB
 
     options->key = xstrdup("_source");
     options->value = xstrdup("client/dbus");
+    if(!options->key || !options->value) {
+        ErrorF("[config] couldn't allocate first key/value pair\n");
+        ret = BadAlloc;
+        goto unwind;
+    }
 
     while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_ARRAY) {
         tmpo = (InputOption *) xcalloc(sizeof(InputOption), 1);
@@ -118,6 +123,8 @@ configAddDevice(DBusMessage *message, DB
             ret = BadAlloc;
             goto unwind;
         }
+        tmpo->next = options;
+        options = tmpo;
 
         dbus_message_iter_recurse(iter, &subiter);
 
@@ -132,8 +139,8 @@ configAddDevice(DBusMessage *message, DB
                    tmp);
             MALFORMED_MESSAGE();
         }
-        tmpo->key = xstrdup(tmp);
-        if (!tmpo->key) {
+        options->key = xstrdup(tmp);
+        if (!options->key) {
             ErrorF("[config] couldn't duplicate key!\n");
             ret = BadAlloc;
             goto unwind;
@@ -148,15 +155,13 @@ configAddDevice(DBusMessage *message, DB
         dbus_message_iter_get_basic(&subiter, &tmp);
         if (!tmp)
             MALFORMED_MESSAGE();
-        tmpo->value = xstrdup(tmp);
-        if (!tmpo->value) {
+        options->value = xstrdup(tmp);
+        if (!options->value) {
             ErrorF("[config] couldn't duplicate option!\n");
             ret = BadAlloc;
             goto unwind;
         }
 
-        tmpo->next = options;
-        options = tmpo;
         dbus_message_iter_next(iter);
     }
 
@@ -164,16 +169,8 @@ configAddDevice(DBusMessage *message, DB
     if (ret != Success)
         DebugF("[config] NewInputDeviceRequest failed\n");
 
-    return ret;
-
+    /* Fall through, must deallocate memory we've allocated */
 unwind:
-    if (tmpo->key)
-        xfree(tmpo->key);
-    if (tmpo->value)
-        xfree(tmpo->value);
-    if (tmpo)
-        xfree(tmpo);
-
     while (options) {
         tmpo = options;
         options = options->next;



More information about the xorg mailing list