[glib/gvariant-varargs] mclasen fixes



commit 4d290b83682084c68a3fa1698f5aa684db78b9a0
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Mar 7 21:36:15 2010 -0500

    mclasen fixes

 glib/gvariant.c |  121 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 67 insertions(+), 54 deletions(-)
---
diff --git a/glib/gvariant.c b/glib/gvariant.c
index 0f5e884..9fb32cb 100644
--- a/glib/gvariant.c
+++ b/glib/gvariant.c
@@ -2680,7 +2680,7 @@ g_variant_builder_end (GVariantBuilder *builder)
 /**
  * g_variant_format_string_scan:
  * @string: a string that may be prefixed with a format string
- * @limit: a pointer to the end of @string
+ * @limit: a pointer to the end of @string, or %NULL
  * @endptr: location to store the end pointer, or %NULL
  * @returns: %TRUE if there was a valid format string
  *
@@ -2696,7 +2696,10 @@ g_variant_builder_end (GVariantBuilder *builder)
  * not be accessed and the effect is otherwise equivalent to if the
  * character at @limit were nul.
  *
- * See XXX for format string infoz.
+ * See the section on <link linkend='gvariant-format-strings'>GVariant
+ * Format Strings</link>.
+ *
+ * Since: 2.24
  **/
 gboolean
 g_variant_format_string_scan (const gchar  *string,
@@ -2705,11 +2708,6 @@ g_variant_format_string_scan (const gchar  *string,
 {
 #define next_char() (string == limit ? '\0' : *string++)
 #define peek_char() (string == limit ? '\0' : *string)
-  /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
-   *    The terminating null character is considered to be
-   *    part of the string.
-   */
-#define is_elem(c,str) ((c) != '\0' && strchr (str, c) != NULL)
   char c;
 
   switch (next_char())
@@ -2740,7 +2738,11 @@ g_variant_format_string_scan (const gchar  *string,
       if (c == '@')
         c = next_char ();
 
-      if (!is_elem (c, "bynqiuxthdsog?"))
+      /* ISO/IEC 9899:1999 (C99) §7.21.5.2:
+       *    The terminating null character is considered to be
+       *    part of the string.
+       */
+      if (c != '\0' && strchr ("bynqiuxthdsog?", c) == NULL)
         return FALSE;
 
       if (!g_variant_format_string_scan (string, limit, &string))
@@ -2782,7 +2784,6 @@ g_variant_format_string_scan (const gchar  *string,
 
 #undef next_char
 #undef peek_char
-#undef is_elem
 
   return TRUE;
 }
@@ -2803,6 +2804,8 @@ g_variant_format_string_scan (const gchar  *string,
  *
  * This function is otherwise exactly like
  * g_variant_format_string_scan().
+ *
+ * Since: 2.24
  **/
 GVariantType *
 g_variant_format_string_scan_type (const gchar  *string,
@@ -2832,11 +2835,9 @@ g_variant_format_string_scan_type (const gchar  *string,
 }
 
 static gboolean
-g_variant_check_valid_format (const gchar *function,
-                              const gchar *location,
-                              const gchar *format_string,
-                              gboolean     single,
-                              GVariant    *value)
+valid_format_string (const gchar *format_string,
+                     gboolean     single,
+                     GVariant    *value)
 {
   const gchar *endptr;
   GVariantType *type;
@@ -2845,15 +2846,12 @@ g_variant_check_valid_format (const gchar *function,
 
   if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))
     {
-      if (g_str_has_prefix (function, "IA__"))
-        function += 4;
-
       if (single)
-        g_critical ("%s: %s: `%s' is not a valid GVariant format string",
-                    location, function, format_string);
+        g_critical ("`%s' is not a valid GVariant format string",
+                    format_string);
       else
-        g_critical ("%s: %s: `%s' does not have a valid GVariant format "
-                    "string as a prefix", location, function, format_string);
+        g_critical ("`%s' does not have a valid GVariant format "
+                    "string as a prefix", format_string);
 
       if (type != NULL)
         g_variant_type_free (type);
@@ -2869,10 +2867,9 @@ g_variant_check_valid_format (const gchar *function,
       fragment = g_strndup (format_string, endptr - format_string);
       typestr = g_variant_type_dup_string (type);
 
-      g_critical ("%s: %s: the GVariant format string `%s' has a type of "
+      g_critical ("the GVariant format string `%s' has a type of "
                   "`%s' but the given value has a type of `%s'",
-                  location, function, fragment, typestr,
-                  g_variant_get_type_string (value));
+                  fragment, typestr, g_variant_get_type_string (value));
 
       g_variant_type_free (type);
 
@@ -2884,13 +2881,6 @@ g_variant_check_valid_format (const gchar *function,
   return TRUE;
 }
 
-#define CHECK_VALID_FORMAT(str,single,value,retval) \
-  G_STMT_START {                                                             \
-    if G_UNLIKELY (!g_variant_check_valid_format (G_STRFUNC, G_STRLOC,       \
-                                                  str, single, value))       \
-      return retval;                                                         \
-  } G_STMT_END
-
 /* Variable Arguments {{{1 */
 #undef DEBUG_GVARIANT_VARARGS
 
@@ -2977,11 +2967,16 @@ g_variant_valist_free_nnp (const gchar *str,
         g_free (ptr);
       break;
 
-    case 's': case 'o': case 'g':
+    case 's':
+    case 'o':
+    case 'g':
       g_free (ptr);
       break;
 
-    case '@': case '*': case '?': case 'v':
+    case '@':
+    case '*':
+    case '?':
+    case 'v':
       g_variant_unref (ptr);
       break;
 
@@ -3042,9 +3037,9 @@ g_variant_valist_new_nnp (const gchar **str,
         const GVariantType *type;
         GVariantType *array_type;
         GVariant **children;
+        gchar **strv = ptr;
         GVariant *value;
         guint length, i;
-        gchar **strv;
 
         if ((*str)[1] == '&')    /* '^a&s' */
           (*str) += 2;
@@ -3053,7 +3048,7 @@ g_variant_valist_new_nnp (const gchar **str,
 
         type = (GVariantType *) (*str)++;
         array_type = g_variant_type_new_array (type);
-        length = g_strv_length (strv = ptr);
+        length = g_strv_length (strv);
         children = g_new (GVariant *, length);
         for (i = 0; i < length; i++)
           children[i] = g_variant_ref_sink (
@@ -3118,7 +3113,9 @@ g_variant_valist_get_nnp (const gchar **str,
       (*str)++;
       return (gchar *) g_variant_get_string (value, NULL);
 
-    case 's': case 'o': case 'g':
+    case 's':
+    case 'o':
+    case 'g':
       return g_variant_dup_string (value, NULL);
 
     case '^':
@@ -3137,7 +3134,9 @@ g_variant_valist_get_nnp (const gchar **str,
       g_variant_type_string_scan (*str, NULL, str);
       /* fall through */
 
-    case '*': case '?': case 'r':
+    case '*':
+    case '?':
+    case 'r':
       return g_variant_ref (value);
 
     case 'v':
@@ -3162,12 +3161,18 @@ g_variant_valist_skip_leaf (const gchar **str,
 
   switch (*(*str)++)
     {
-    case 'b': case 'y': case 'n': case 'q':
-    case 'i': case 'u': case 'h':
+    case 'b':
+    case 'y':
+    case 'n':
+    case 'q':
+    case 'i':
+    case 'u':
+    case 'h':
       get_arg (*app, int);
       return;
 
-    case 'x': case 't':
+    case 'x':
+    case 't':
       get_arg (*app, guint64);
       return;
 
@@ -3308,16 +3313,22 @@ g_variant_valist_get_leaf (const gchar **str,
           *(guchar *) ptr = 0;
           return;
 
-        case 'n': case 'q':
+        case 'n':
+        case 'q':
           *(guint16 *) ptr = 0;
           return;
 
-        case 'i': case 'u': case 'h': case 'b':
+        case 'i':
+        case 'u':
+        case 'h':
+        case 'b':
           g_assert (sizeof (gboolean) == sizeof (guint32));
           *(guint32 *) ptr = 0;
           return;
 
-        case 'x': case 't': case 'd':
+        case 'x':
+        case 't':
+        case 'd':
           g_assert (sizeof (gdouble) == sizeof (guint64));
           *(guint64 *) ptr = 0;
           return;
@@ -3496,9 +3507,8 @@ g_variant_new (const gchar *format_string,
   GVariant *value;
   va_list ap;
 
-  CHECK_VALID_FORMAT(format_string, TRUE, NULL, NULL);
-
-  g_return_val_if_fail (format_string[0] != '?' && format_string[0] != '@' &&
+  g_return_val_if_fail (valid_format_string (format_string, TRUE, NULL) &&
+                        format_string[0] != '?' && format_string[0] != '@' &&
                         format_string[0] != '*' && format_string[0] != 'r',
                         NULL);
 
@@ -3560,8 +3570,8 @@ g_variant_new_va (gpointer      must_be_null,
 {
   GVariant *value;
 
-  CHECK_VALID_FORMAT(format_string, endptr == NULL, NULL, NULL);
-
+  g_return_val_if_fail (valid_format_string (format_string, !endptr, NULL),
+                        NULL);
   g_return_val_if_fail (must_be_null == NULL, NULL);
   g_return_val_if_fail (app != NULL, NULL);
 
@@ -3600,7 +3610,7 @@ g_variant_get (GVariant    *value,
 {
   va_list ap;
 
-  CHECK_VALID_FORMAT(format_string, TRUE, value,);
+  g_return_if_fail (valid_format_string (format_string, TRUE, value));
 
   /* if any direct-pointer-access formats are in use, flatten first */
   if (strchr (format_string, '&'))
@@ -3648,12 +3658,11 @@ g_variant_get_va (GVariant     *value,
                   const gchar **endptr,
                   va_list      *app)
 {
+  g_return_if_fail (valid_format_string (format_string, !endptr, value));
   g_return_if_fail (must_be_null == NULL);
   g_return_if_fail (value != NULL);
   g_return_if_fail (app != NULL);
 
-  CHECK_VALID_FORMAT(format_string, endptr == NULL, value,);
-
   /* if any direct-pointer-access formats are in use, flatten first */
   if (strchr (format_string, '&'))
     g_variant_get_data (value);
@@ -3741,8 +3750,7 @@ g_variant_get_child (GVariant    *value,
   va_list ap;
 
   child = g_variant_get_child_value (value, index_);
-
-  CHECK_VALID_FORMAT(format_string, TRUE, child,);
+  g_return_if_fail (valid_format_string (format_string, TRUE, child));
 
   va_start (ap, format_string);
   g_variant_get_va (child, NULL, format_string, NULL, &ap);
@@ -3808,7 +3816,8 @@ g_variant_iter_next (GVariantIter *iter,
 
   value = g_variant_iter_next_value (iter);
 
-  CHECK_VALID_FORMAT(format_string, TRUE, value, FALSE);
+  g_return_val_if_fail (valid_format_string (format_string, TRUE, value),
+                        FALSE);
 
   if (value != NULL)
     {
@@ -3904,6 +3913,10 @@ g_variant_iter_loop (GVariantIter *iter,
 
   value = g_variant_iter_next_value (iter);
 
+  g_return_val_if_fail (!first_time ||
+                        valid_format_string (format_string, TRUE, value),
+                        FALSE);
+
   va_start (ap, format_string);
   g_variant_valist_get (&format_string, value, !first_time, &ap);
   va_end (ap);



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