[glib/GVariantType] GVariantType: clean up g_return checks, fix PLT



commit a9f4788eb2b4a2ca4c95c59682dd827b93900848
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Jan 22 16:40:28 2010 -0500

    GVariantType: clean up g_return checks, fix PLT

 glib/glib.symbols   |    1 +
 glib/gvarianttype.c |  153 ++++++++++++++++++++++++++++++++++++++++-----------
 glib/gvarianttype.h |   21 +++----
 3 files changed, 130 insertions(+), 45 deletions(-)
---
diff --git a/glib/glib.symbols b/glib/glib.symbols
index f15eb58..ad2b509 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -1674,6 +1674,7 @@ g_variant_type_new_array
 g_variant_type_new_maybe
 g_variant_type_new_tuple
 g_variant_type_new_dict_entry
+g_variant_type_checked_
 #endif
 #endif
 
diff --git a/glib/gvarianttype.c b/glib/gvarianttype.c
index 0abb0b3..a8ef945 100644
--- a/glib/gvarianttype.c
+++ b/glib/gvarianttype.c
@@ -5,7 +5,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2 of the licence, 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
@@ -503,6 +503,22 @@
  * Since 2.24
  **/ 
 
+static gboolean
+g_variant_type_check (const GVariantType *type)
+{
+  const gchar *type_string;
+
+  if (type == NULL)
+    return FALSE;
+
+  type_string = (const gchar *) type;
+#ifndef G_DISABLE_CHECKS
+  return g_variant_type_string_scan (type_string, NULL, NULL);
+#else
+  return TRUE;
+#endif
+}
+
 /**
  * g_variant_type_string_scan:
  * @string: a pointer to any string
@@ -531,6 +547,8 @@ g_variant_type_string_scan (const gchar  *string,
                             const gchar  *limit,
                             const gchar **endptr)
 {
+  g_return_val_if_fail (string != NULL, FALSE);
+
   if (string == limit || *string == '\0')
     return FALSE;
 
@@ -590,6 +608,8 @@ g_variant_type_string_scan (const gchar  *string,
 gboolean
 g_variant_type_string_is_valid (const gchar *type_string)
 {
+  g_return_val_if_fail (type_string != NULL, FALSE);
+
   if (!g_variant_type_string_scan (type_string, NULL, NULL))
     return FALSE;
 
@@ -611,6 +631,8 @@ g_variant_type_string_is_valid (const gchar *type_string)
 void
 g_variant_type_free (GVariantType *type)
 {
+  g_return_if_fail (type == NULL || g_variant_type_check (type));
+
   g_free (type);
 }
 
@@ -630,6 +652,8 @@ g_variant_type_copy (const GVariantType *type)
   gsize length;
   gchar *new;
 
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
+
   length = g_variant_type_get_string_length (type);
   new = g_malloc (length + 1);
 
@@ -654,6 +678,8 @@ g_variant_type_copy (const GVariantType *type)
 GVariantType *
 g_variant_type_new (const gchar *type_string)
 {
+  g_return_val_if_fail (type_string != NULL, NULL);
+
   return g_variant_type_copy (G_VARIANT_TYPE (type_string));
 }
 
@@ -675,6 +701,8 @@ g_variant_type_get_string_length (const GVariantType *type)
   gint brackets = 0;
   gsize index = 0;
 
+  g_return_val_if_fail (g_variant_type_check (type), 0);
+
   do
     {
       while (type_string[index] == 'a' || type_string[index] == 'm')
@@ -709,6 +737,8 @@ g_variant_type_get_string_length (const GVariantType *type)
 const gchar *
 g_variant_type_peek_string (const GVariantType *type)
 {
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
+
   return (const gchar *) type;
 }
 
@@ -726,6 +756,8 @@ g_variant_type_peek_string (const GVariantType *type)
 gchar *
 g_variant_type_dup_string (const GVariantType *type)
 {
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
+
   return g_strndup (g_variant_type_peek_string (type),
                     g_variant_type_get_string_length (type));
 }
@@ -751,10 +783,15 @@ g_variant_type_dup_string (const GVariantType *type)
 gboolean
 g_variant_type_is_definite (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
-  gsize type_length = g_variant_type_get_string_length (type);
+  const gchar *type_string;
+  gsize type_length;
   gsize i;
 
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+
+  type_length = g_variant_type_get_string_length (type);
+  type_string = g_variant_type_peek_string (type);
+
   for (i = 0; i < type_length; i++)
     if (type_string[i] == '*' ||
         type_string[i] == '?' ||
@@ -783,8 +820,11 @@ g_variant_type_is_definite (const GVariantType *type)
 gboolean
 g_variant_type_is_container (const GVariantType *type)
 {
-  gchar first_char = g_variant_type_peek_string (type)[0];
+  gchar first_char;
+
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
 
+  first_char = g_variant_type_peek_string (type)[0];
   switch (first_char)
   {
     case 'a':
@@ -820,8 +860,11 @@ g_variant_type_is_container (const GVariantType *type)
 gboolean
 g_variant_type_is_basic (const GVariantType *type)
 {
-  gchar first_char = g_variant_type_peek_string (type)[0];
+  gchar first_char;
 
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+
+  first_char = g_variant_type_peek_string (type)[0];
   switch (first_char)
   {
     case 'b':
@@ -862,6 +905,8 @@ g_variant_type_is_basic (const GVariantType *type)
 gboolean
 g_variant_type_is_maybe (const GVariantType *type)
 {
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+
   return g_variant_type_peek_string (type)[0] == 'm';
 }
 
@@ -882,6 +927,8 @@ g_variant_type_is_maybe (const GVariantType *type)
 gboolean
 g_variant_type_is_array (const GVariantType *type)
 {
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+
   return g_variant_type_peek_string (type)[0] == 'a';
 }
 
@@ -903,8 +950,11 @@ g_variant_type_is_array (const GVariantType *type)
 gboolean
 g_variant_type_is_tuple (const GVariantType *type)
 {
-  gchar type_char = g_variant_type_peek_string (type)[0];
+  gchar type_char;
+
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
 
+  type_char = g_variant_type_peek_string (type)[0];
   return type_char == 'r' || type_char == '(';
 }
 
@@ -925,6 +975,8 @@ g_variant_type_is_tuple (const GVariantType *type)
 gboolean
 g_variant_type_is_dict_entry (const GVariantType *type)
 {
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+
   return g_variant_type_peek_string (type)[0] == '{';
 }
 
@@ -944,11 +996,16 @@ g_variant_type_is_dict_entry (const GVariantType *type)
 guint
 g_variant_type_hash (gconstpointer type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
-  gsize length = g_variant_type_get_string_length (type);
+  const gchar *type_string;
   guint value = 0;
+  gsize length;
   gsize i;
 
+  g_return_val_if_fail (g_variant_type_check (type), 0);
+
+  type_string = g_variant_type_peek_string (type);
+  length = g_variant_type_get_string_length (type);
+
   for (i = 0; i < length; i++)
     value = (value << 5) - value + type_string[i];
 
@@ -981,6 +1038,9 @@ g_variant_type_equal (gconstpointer type1,
   const gchar *string1, *string2;
   gsize size1, size2;
 
+  g_return_val_if_fail (g_variant_type_check (type1), FALSE);
+  g_return_val_if_fail (g_variant_type_check (type2), FALSE);
+
   if (type1 == type2)
     return TRUE;
 
@@ -1018,10 +1078,14 @@ g_variant_type_is_subtype_of (const GVariantType *type,
   const gchar *supertype_end;
   const gchar *type_string;
 
+  g_return_val_if_fail (g_variant_type_check (type), FALSE);
+  g_return_val_if_fail (g_variant_type_check (supertype), FALSE);
+
   supertype_string = g_variant_type_peek_string (supertype);
   type_string = g_variant_type_peek_string (type);
 
-  supertype_end = supertype_string + g_variant_type_get_string_length (supertype);
+  supertype_end = supertype_string +
+                  g_variant_type_get_string_length (supertype);
 
   /* we know that type and supertype are both well-formed, so it's
    * safe to treat this merely as a text processing problem.
@@ -1080,7 +1144,11 @@ g_variant_type_is_subtype_of (const GVariantType *type,
 const GVariantType *
 g_variant_type_element (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
+  const gchar *type_string;
+
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
+ 
+  type_string = g_variant_type_peek_string (type);
 
   g_assert (type_string[0] == 'a' || type_string[0] == 'm');
 
@@ -1112,8 +1180,11 @@ g_variant_type_element (const GVariantType *type)
 const GVariantType *
 g_variant_type_first (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
+  const gchar *type_string;
+ 
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
 
+  type_string = g_variant_type_peek_string (type);
   g_assert (type_string[0] == '(' || type_string[0] == '{');
 
   if (type_string[1] == ')')
@@ -1144,8 +1215,11 @@ g_variant_type_first (const GVariantType *type)
 const GVariantType *
 g_variant_type_next (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
+  const gchar *type_string;
+
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
 
+  type_string = g_variant_type_peek_string (type);
   type_string += g_variant_type_get_string_length (type);
 
   if (*type_string == ')' || *type_string == '}')
@@ -1176,6 +1250,8 @@ g_variant_type_n_items (const GVariantType *type)
 {
   gsize count = 0;
 
+  g_return_val_if_fail (g_variant_type_check (type), 0);
+
   for (type = g_variant_type_first (type);
        type;
        type = g_variant_type_next (type))
@@ -1200,8 +1276,11 @@ g_variant_type_n_items (const GVariantType *type)
 const GVariantType *
 g_variant_type_key (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
+  const gchar *type_string;
+
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
 
+  type_string = g_variant_type_peek_string (type);
   g_assert (type_string[0] == '{');
 
   return (const GVariantType *) &type_string[1];
@@ -1221,8 +1300,11 @@ g_variant_type_key (const GVariantType *type)
 const GVariantType *
 g_variant_type_value (const GVariantType *type)
 {
-  const gchar *type_string = g_variant_type_peek_string (type);
+  const gchar *type_string;
 
+  g_return_val_if_fail (g_variant_type_check (type), NULL);
+
+  type_string = g_variant_type_peek_string (type);
   g_assert (type_string[0] == '{');
 
   return g_variant_type_next (g_variant_type_key (type));
@@ -1231,11 +1313,14 @@ g_variant_type_value (const GVariantType *type)
 /**
  * g_variant_type_new_tuple:
  * @items: an array of #GVariantTypes, one for each item
- * @length: the length of @items
+ * @length: the length of @items, or -1
  * @returns: a new tuple #GVariantType
  *
  * Constructs a new tuple type, from @items.
  *
+ * @length is the number of items in @items, or -1 to indicate that
+ * @items is %NULL-terminated.
+ *
  * It is appropriate to call g_variant_type_free() on the return value.
  *
  * Since 2.24
@@ -1257,6 +1342,8 @@ g_variant_type_new_tuple_slow (const GVariantType * const *items,
       const GVariantType *type;
       gsize size;
 
+      g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
+
       type = items[i];
       size = g_variant_type_get_string_length (type);
       g_string_append_len (string, (const gchar *) type, size);
@@ -1274,13 +1361,10 @@ g_variant_type_new_tuple (const GVariantType * const *items,
   gsize offset;
   gsize i;
 
-#ifdef GVARIANT_DEBUG
-    for (i = 0; i < length; i++)
-      if (func)
-        g_assert (g_variant_type_check (func (items[i])));
-      else
-        g_assert (g_variant_type_check (items[i]));
-#endif
+  g_return_val_if_fail (length == 0 || items != NULL, NULL);
+
+  if (length < 0)
+    for (length = 0; items[length] != NULL; length++);
 
   offset = 0;
   buffer[offset++] = '(';
@@ -1290,6 +1374,8 @@ g_variant_type_new_tuple (const GVariantType * const *items,
       const GVariantType *type;
       gsize size;
 
+      g_return_val_if_fail (g_variant_type_check (items[i]), NULL);
+
       type = items[i];
       size = g_variant_type_get_string_length (type);
 
@@ -1324,9 +1410,7 @@ g_variant_type_new_array (const GVariantType *element)
   gsize size;
   gchar *new;
 
-#ifdef GVARIANT_DEBUG
-    g_assert (g_variant_type_check (element));
-#endif
+  g_return_val_if_fail (g_variant_type_check (element), NULL);
 
   size = g_variant_type_get_string_length (element);
   new = g_malloc (size + 1);
@@ -1355,9 +1439,7 @@ g_variant_type_new_maybe (const GVariantType *element)
   gsize size;
   gchar *new;
 
-#ifdef GVARIANT_DEBUG
-    g_assert (g_variant_type_check (element));
-#endif
+  g_return_val_if_fail (g_variant_type_check (element), NULL);
 
   size = g_variant_type_get_string_length (element);
   new = g_malloc (size + 1);
@@ -1388,11 +1470,8 @@ g_variant_type_new_dict_entry (const GVariantType *key,
   gsize keysize, valsize;
   gchar *new;
 
-#ifdef GVARIANT_DEBUG
-    g_assert (g_variant_type_check (key));
-    g_assert (g_variant_type_check (value));
-    g_assert (g_variant_type_is_basic (key));
-#endif
+  g_return_val_if_fail (g_variant_type_check (key), NULL);
+  g_return_val_if_fail (g_variant_type_check (value), NULL);
 
   keysize = g_variant_type_get_string_length (key);
   valsize = g_variant_type_get_string_length (value);
@@ -1407,5 +1486,13 @@ g_variant_type_new_dict_entry (const GVariantType *key,
   return (GVariantType *) new;
 }
 
+/* private */
+const GVariantType *
+g_variant_type_checked_ (const gchar *type_string)
+{
+  g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
+  return (const GVariantType *) type_string;
+}
+
 #define __G_VARIANT_TYPE_C__
 #include "galiasdef.c"
diff --git a/glib/gvarianttype.h b/glib/gvarianttype.h
index 43e53e9..4fbf420 100644
--- a/glib/gvarianttype.h
+++ b/glib/gvarianttype.h
@@ -5,7 +5,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2 of the licence, 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
@@ -216,6 +216,12 @@ typedef struct _GVariantType GVariantType;
  **/
 #define G_VARIANT_TYPE_DICTIONARY           ((const GVariantType *) "a{?*}")
 
+#ifndef G_DISABLE_CHECKS
+# define G_VARIANT_TYPE(str)                (g_variant_type_checked_ ((str)))
+#else
+# define G_VARIANT_TYPE(str)                ((const GVariantType *) (str))
+#endif
+
 G_BEGIN_DECLS
 
 /* type string checking */
@@ -268,17 +274,8 @@ GVariantType *                  g_variant_type_new_tuple                (const G
 GVariantType *                  g_variant_type_new_dict_entry           (const GVariantType  *key,
                                                                          const GVariantType  *value);
 
-#ifdef G_CAN_INLINE
-static inline const GVariantType *
-_g_variant_type (const gchar *type_string)
-{
-  g_return_val_if_fail (g_variant_type_string_is_valid (type_string), NULL);
-  return (const GVariantType *) type_string;
-}
-#define G_VARIANT_TYPE(type_string) _g_variant_type((type_string))
-#else
-#define G_VARIANT_TYPE(type_string) ((const GVariantType *) (type_string))
-#endif
+/*< private >*/
+const GVariantType *            g_variant_type_checked_                 (const gchar *);
 
 G_END_DECLS
 



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