[prevent][24443] Fix for resource leakage
[platform/upstream/at-spi2-core.git] / dbind / dbind-any.c
index 83a87ba..fdde7b6 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Copyright 2008-2011 Novell, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
 /* type driven marshalling */
 #include <stdio.h>
 #include <glib.h>
 
 #undef DEBUG
 
-/* Align a value upward to a boundary, expressed as a number of bytes.
-   E.g. align to an 8-byte boundary with argument of 8.  */
-
-/*
+/*  Align a value upward to a boundary, expressed as a number of bytes.
+ *  E.g. align to an 8-byte boundary with argument of 8.
+ *
  *   (this + boundary - 1)
  *          &
  *    ~(boundary - 1)
  */
-
 #define ALIGN_VALUE(this, boundary) \
   (( ((gulong)(this)) + (((gulong)(boundary)) -1)) & (~(((gulong)(boundary))-1)))
 
@@ -49,9 +66,9 @@ warn_braces ()
 /*---------------------------------------------------------------------------*/
 
 static unsigned int
-dbind_find_c_alignment_r (char **type)
+dbind_find_c_alignment_r (const char **type)
 {
-        unsigned int retval = 1;
+    unsigned int retval = 1;
 
     char t = **type;
     (*type)++;
@@ -82,7 +99,8 @@ dbind_find_c_alignment_r (char **type)
     case DBUS_TYPE_SIGNATURE:
     case DBUS_TYPE_ARRAY:
         return DBIND_ALIGNOF_DBIND_POINTER;
-        case DBUS_STRUCT_BEGIN_CHAR:
+    case DBUS_STRUCT_BEGIN_CHAR:
+      /* TODO: I think this would break with a nested struct */
 #if DBIND_ALIGNOF_DBIND_STRUCT > 1
                 retval = MAX (retval, DBIND_ALIGNOF_DBIND_STRUCT);
 #endif
@@ -91,7 +109,17 @@ dbind_find_c_alignment_r (char **type)
                         retval = MAX (retval, elem_align);
         }
         (*type)++;
-                return retval;
+        return retval;
+    case DBUS_DICT_ENTRY_BEGIN_CHAR:
+#if DBIND_ALIGNOF_DBIND_STRUCT > 1
+                retval = MAX (retval, DBIND_ALIGNOF_DBIND_STRUCT);
+#endif
+        while (**type != DBUS_DICT_ENTRY_END_CHAR) {
+            int elem_align = dbind_find_c_alignment_r (type);
+                        retval = MAX (retval, elem_align);
+        }
+        (*type)++;
+        return retval;
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
         warn_braces ();
@@ -99,34 +127,40 @@ dbind_find_c_alignment_r (char **type)
     case '\0':
         g_assert_not_reached();
         break;
-        default:
+    default:
                 return 1;
-        }
+  }
 }
 
 /*---------------------------------------------------------------------------*/
 
 /* gather immediate allocation information for this type */
 static size_t
-dbind_gather_alloc_info_r (char **type)
+dbind_gather_alloc_info_r (const char **type)
 {
-    char t = **type;
-    (*type)++;
-    if (t == DBUS_TYPE_ARRAY) {
-        switch (**type) {
-            case DBUS_STRUCT_BEGIN_CHAR:
-                while (**type != DBUS_STRUCT_END_CHAR && **type != '\0') (*type)++;
-                if (**type != '\0') (*type)++;
-                break;
-            case '\0':
-                break;
-            default:
-                (*type)++;
-                break;
-            }
+  char t = **type;
+  (*type)++;
+  if (t == DBUS_TYPE_ARRAY)
+    {
+      switch (**type)
+        {
+          case DBUS_STRUCT_BEGIN_CHAR:
+              while (**type != DBUS_STRUCT_END_CHAR && **type != '\0') (*type)++;
+              if (**type != '\0') (*type)++;
+              break;
+          case DBUS_DICT_ENTRY_BEGIN_CHAR:
+              while (**type != DBUS_DICT_ENTRY_END_CHAR && **type != '\0') (*type)++;
+              if (**type != '\0') (*type)++;
+              break;
+          case '\0':
+              break;
+          default:
+              (*type)++;
+              break;
         }
+    }
 
-        switch (t) {
+  switch (t) {
     case DBUS_TYPE_BYTE:
         return sizeof (char);
     case DBUS_TYPE_BOOLEAN:
@@ -164,27 +198,41 @@ dbind_gather_alloc_info_r (char **type)
 
                 return sum;
     }
+    case DBUS_DICT_ENTRY_BEGIN_CHAR: {
+                int sum = 0, stralign;
+
+        stralign = dbind_find_c_alignment (*type - 1);
+
+        while (**type != DBUS_DICT_ENTRY_END_CHAR) {
+                        sum = ALIGN_VALUE (sum, dbind_find_c_alignment (*type));
+                        sum += dbind_gather_alloc_info_r (type);
+        }
+                sum = ALIGN_VALUE (sum, stralign);
+
+        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
+        (*type)++;
+
+                return sum;
+    }
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
         warn_braces ();
-        default:
-                return 0;
-        }
+    default:
+        return 0;
+  }
 }
 
 static size_t
-dbind_gather_alloc_info (char *type)
+dbind_gather_alloc_info (const char *type)
 {
-    return dbind_gather_alloc_info_r (&type);
+  return dbind_gather_alloc_info_r (&type);
 }
 
 /*---------------------------------------------------------------------------*/
 
 static void
-dbind_any_free_r (char **type, void **data)
+dbind_any_free_r (const char **type, void **data)
 {
-    size_t len;
-
 #ifdef DEBUG
     fprintf (stderr, "any free '%c' to %p\n", **type, *data);
 #endif
@@ -208,7 +256,7 @@ dbind_any_free_r (char **type, void **data)
         int i;
         GArray *vals = **(void ***)data;
         size_t elem_size, elem_align;
-        char *saved_child_type, *child_type_string;
+        const char *saved_child_type;
 
         (*type)++;
         saved_child_type = *type;
@@ -234,7 +282,7 @@ dbind_any_free_r (char **type, void **data)
 
         offset = 0 ;
         while (**type != DBUS_STRUCT_END_CHAR) {
-            char *subt = *type;
+            const char *subt = *type;
                         offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
                         *data = PTR_PLUS (data0, offset);
             dbind_any_free_r (type, data);
@@ -249,6 +297,30 @@ dbind_any_free_r (char **type, void **data)
 
         break;
     }
+    case DBUS_DICT_ENTRY_BEGIN_CHAR: {
+                gconstpointer data0 = *data;
+                int offset = 0, stralign;
+
+        stralign = dbind_find_c_alignment (*type);
+        (*type)++;
+
+        offset = 0 ;
+        while (**type != DBUS_DICT_ENTRY_END_CHAR) {
+            const char *subt = *type;
+                        offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
+                        *data = PTR_PLUS (data0, offset);
+            dbind_any_free_r (type, data);
+            offset += dbind_gather_alloc_info (subt);
+        }
+
+                offset = ALIGN_VALUE (offset, stralign);
+                *data = PTR_PLUS (data0, offset);
+
+        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
+        (*type)++;
+
+        break;
+    }
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
         warn_braces ();
@@ -260,7 +332,7 @@ dbind_any_free_r (char **type, void **data)
 
 void
 dbind_any_marshal (DBusMessageIter *iter,
-                   char           **type,
+                   const char           **type,
                    void           **data)
 {
     size_t len;
@@ -284,7 +356,8 @@ dbind_any_marshal (DBusMessageIter *iter,
         GArray *vals = **(void ***)data;
         size_t elem_size, elem_align;
         DBusMessageIter sub;
-        char *saved_child_type, *child_type_string;
+        const char *saved_child_type;
+        char *child_type_string;
 
         (*type)++;
         saved_child_type = *type;
@@ -321,7 +394,7 @@ dbind_any_marshal (DBusMessageIter *iter,
 
         offset = 0 ;
         while (**type != DBUS_STRUCT_END_CHAR) {
-            char *subt = *type;
+            const char *subt = *type;
                         offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
                         *data = PTR_PLUS (data0, offset);
             dbind_any_marshal (&sub, type, data);
@@ -338,6 +411,36 @@ dbind_any_marshal (DBusMessageIter *iter,
 
         break;
     }
+    case DBUS_DICT_ENTRY_BEGIN_CHAR: {
+                gconstpointer data0 = *data;
+                int offset = 0, stralign;
+        DBusMessageIter sub;
+
+        stralign = dbind_find_c_alignment (*type);
+
+        (*type)++;
+
+        dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub);
+
+        offset = 0 ;
+        while (**type != DBUS_DICT_ENTRY_END_CHAR) {
+            const char *subt = *type;
+                        offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
+                        *data = PTR_PLUS (data0, offset);
+            dbind_any_marshal (&sub, type, data);
+            offset += dbind_gather_alloc_info (subt);
+        }
+
+                offset = ALIGN_VALUE (offset, stralign);
+                *data = PTR_PLUS (data0, offset);
+
+        dbus_message_iter_close_container (iter, &sub);
+
+        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
+        (*type)++;
+
+        break;
+    }
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
         warn_braces ();
@@ -349,10 +452,17 @@ dbind_any_marshal (DBusMessageIter *iter,
 
 void
 dbind_any_marshal_va (DBusMessageIter *iter,
-                      char           **arg_types,
+                      const char           **arg_types,
                       va_list          args)
 {
-    char *p = *arg_types;
+    const char *p = *arg_types;
+
+    /* Guard against null arg types 
+       Fix for - http://bugs.freedesktop.org/show_bug.cgi?id=23027
+     */
+    if (p == NULL)
+        p = "";
+
     {
         /* special case base-types since we need to walk the stack worse-luck */
         for (;*p != '\0' && *p != '=';) {
@@ -394,6 +504,10 @@ dbind_any_marshal_va (DBusMessageIter *iter,
                 ptrarg = va_arg (args, void *);
                 arg = ptrarg;
                 break;
+            case DBUS_DICT_ENTRY_BEGIN_CHAR:
+                ptrarg = va_arg (args, void *);
+                arg = ptrarg;
+                break;
 
             case DBUS_TYPE_VARIANT:
                 fprintf (stderr, "No variant support yet - very toolkit specific\n");
@@ -407,6 +521,8 @@ dbind_any_marshal_va (DBusMessageIter *iter,
             if (arg != NULL)
                 dbind_any_marshal (iter, &p, &arg);
             }
+        if (*arg_types)
+          *arg_types = p;
     }
 }
 
@@ -414,7 +530,7 @@ dbind_any_marshal_va (DBusMessageIter *iter,
 
 void
 dbind_any_demarshal (DBusMessageIter *iter,
-                     char           **type,
+                     const char           **type,
                      void           **data)
 {
     size_t len;
@@ -446,7 +562,7 @@ dbind_any_demarshal (DBusMessageIter *iter,
         GArray *vals;
         DBusMessageIter child;
         size_t elem_size, elem_align;
-        char *stored_child_type;
+        const char *stored_child_type;
         int i;
 
         (*type)++;
@@ -462,7 +578,7 @@ dbind_any_demarshal (DBusMessageIter *iter,
         dbus_message_iter_recurse (iter, &child);
         while (dbus_message_iter_get_arg_type (&child) != DBUS_TYPE_INVALID) {
             void *ptr;
-            char *subt = stored_child_type;
+            const char *subt = stored_child_type;
             g_array_set_size (vals, i + 1);
             ptr = vals->data + elem_size * i;
             ptr = ALIGN_ADDRESS (ptr, elem_align);
@@ -483,7 +599,7 @@ dbind_any_demarshal (DBusMessageIter *iter,
         dbus_message_iter_recurse (iter, &child);
 
         while (**type != DBUS_STRUCT_END_CHAR) {
-            char *subt = *type;
+            const char *subt = *type;
                         offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
                         *data = PTR_PLUS (data0, offset);
             dbind_any_demarshal (&child, type, data);
@@ -498,6 +614,37 @@ dbind_any_demarshal (DBusMessageIter *iter,
 
         break;
     }
+    case DBUS_DICT_ENTRY_BEGIN_CHAR: {
+                gconstpointer data0 = *data;
+                int offset = 0, stralign;
+        DBusMessageIter child;
+
+        stralign = dbind_find_c_alignment (*type);
+
+        (*type)++;
+
+        dbus_message_iter_recurse (iter, &child);
+
+        while (**type != DBUS_DICT_ENTRY_END_CHAR) {
+            const char *subt = *type;
+                        offset = ALIGN_VALUE (offset, dbind_find_c_alignment (*type));
+                        *data = PTR_PLUS (data0, offset);
+            dbind_any_demarshal (&child, type, data);
+            offset += dbind_gather_alloc_info (subt);
+        }
+
+                offset = ALIGN_VALUE (offset, stralign);
+                *data = PTR_PLUS (data0, offset);
+
+        g_assert (**type == DBUS_DICT_ENTRY_END_CHAR);
+        (*type)++;
+
+        break;
+    case DBUS_TYPE_VARIANT:
+        /* skip; unimplemented for now */
+        (*type)++;
+        break;
+    }
     case DBUS_TYPE_STRUCT:
     case DBUS_TYPE_DICT_ENTRY:
         warn_braces ();
@@ -506,14 +653,103 @@ dbind_any_demarshal (DBusMessageIter *iter,
     dbus_message_iter_next (iter);
 }
 
+static const char *
+pass_complex_arg (const char *p, char begin, char end)
+{
+  int level = 1;
+
+  p++;
+  while (*p && level > 0)
+  {
+    if (*p == begin)
+      level++;
+    else if (*p == end)
+      level--;
+    p++;
+  }
+  if (*p == end)
+    p++;
+  return p;
+}
+
+static const char *
+pass_arg (const char *p)
+{
+  switch (*p)
+  {
+  case '(':
+    return pass_complex_arg (p, '(', ')');
+  case '{':
+    return pass_complex_arg (p, '{', '}');
+  case 'a':
+    return pass_arg (p+1);
+  default:
+    return p + 1;
+  }
+}
+
 /*---------------------------------------------------------------------------*/
 
 void
 dbind_any_demarshal_va (DBusMessageIter *iter,
-                        char           **arg_types,
+                        const char           **arg_types,
                         va_list          args)
 {
-    char *p = *arg_types;
+    const char *p = *arg_types;
+
+        /* Pass in args */
+    for (;*p != '\0' && *p != '=';) {
+        int intarg;
+        void *ptrarg;
+        double doublearg;
+        dbus_int64_t int64arg;
+        void *arg = NULL;
+
+        switch (*p) {
+        case DBUS_TYPE_BYTE:
+        case DBUS_TYPE_BOOLEAN:
+        case DBUS_TYPE_INT16:
+        case DBUS_TYPE_UINT16:
+        case DBUS_TYPE_INT32:
+        case DBUS_TYPE_UINT32:
+            intarg = va_arg (args, int);
+            break;
+        case DBUS_TYPE_INT64:
+        case DBUS_TYPE_UINT64:
+            int64arg = va_arg (args, dbus_int64_t);
+            break;
+        case DBUS_TYPE_DOUBLE:
+            doublearg = va_arg (args, double);
+            break;
+        /* ptr types */
+        case DBUS_TYPE_STRING:
+        case DBUS_TYPE_OBJECT_PATH:
+        case DBUS_TYPE_SIGNATURE:
+        case DBUS_TYPE_ARRAY:
+        case DBUS_TYPE_DICT_ENTRY:
+            ptrarg = va_arg (args, void *);
+            break;
+        case DBUS_STRUCT_BEGIN_CHAR:
+            ptrarg = va_arg (args, void *);
+            break;
+        case DBUS_DICT_ENTRY_BEGIN_CHAR:
+            ptrarg = va_arg (args, void *);
+            break;
+
+        case DBUS_TYPE_VARIANT:
+            fprintf (stderr, "No variant support yet - very toolkit specific\n");
+            ptrarg = va_arg (args, void *);
+            break;
+        default:
+            fprintf (stderr, "Unknown / invalid arg type %c\n", *p);
+            break;
+        }
+      p = pass_arg (p);
+    }
+
+    if (p [0] == '=' && p[1] == '>')
+      p += 2;
+
     for (;*p != '\0';) {
         void *arg = va_arg (args, void *);
         dbind_any_demarshal (iter, &p, &arg);
@@ -524,7 +760,7 @@ dbind_any_demarshal_va (DBusMessageIter *iter,
 
 /* nice deep free ... */
 void
-dbind_any_free (char *type,
+dbind_any_free (const char *type,
                 void *ptr)
 {
     dbind_any_free_r (&type, &ptr);
@@ -532,7 +768,7 @@ dbind_any_free (char *type,
 
 /* should this be the default normalization ? */
 void
-dbind_any_free_ptr (char *type, void *ptr)
+dbind_any_free_ptr (const char *type, void *ptr)
 {
     dbind_any_free (type, &ptr);
 }
@@ -540,7 +776,7 @@ dbind_any_free_ptr (char *type, void *ptr)
 /*---------------------------------------------------------------------------*/
 
 unsigned int
-dbind_find_c_alignment (char *type)
+dbind_find_c_alignment (const char *type)
 {
     return dbind_find_c_alignment_r (&type);
 }