dbus/dbus dbus-marshal-validate.c,1.20,1.21

Waldo Bastian bastian at freedesktop.org
Mon Sep 12 01:19:35 PDT 2005


Update of /cvs/dbus/dbus/dbus
In directory gabe:/tmp/cvs-serv19221/dbus

Modified Files:
	dbus-marshal-validate.c 
Log Message:
* dbus/dbus-marshal-validate.c, doc/dbus-specification.xml,
test/Makefile.am, test/test-names.c: allow hyphens in bus names.


Index: dbus-marshal-validate.c
===================================================================
RCS file: /cvs/dbus/dbus/dbus/dbus-marshal-validate.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- dbus-marshal-validate.c	15 Jun 2005 15:15:32 -0000	1.20
+++ dbus-marshal-validate.c	12 Sep 2005 08:19:33 -0000	1.21
@@ -677,7 +677,7 @@
 }
 
 /**
- * Determine wether the given charater is valid as the first charater
+ * Determine wether the given character is valid as the first character
  * in a name.
  */
 #define VALID_INITIAL_NAME_CHARACTER(c)         \
@@ -686,7 +686,7 @@
     ((c) == '_') )
 
 /**
- * Determine wether the given charater is valid as a second or later
+ * Determine wether the given character is valid as a second or later
  * character in a name
  */
 #define VALID_NAME_CHARACTER(c)                 \
@@ -922,15 +922,47 @@
   return _dbus_validate_interface (str, start, len);
 }
 
-/* This assumes the first char exists and is ':' */
-static dbus_bool_t
-_dbus_validate_unique_name (const DBusString  *str,
-                            int                start,
-                            int                len)
+/**
+ * Determine wether the given character is valid as the first character
+ * in a bus name.
+ */
+#define VALID_INITIAL_BUS_NAME_CHARACTER(c)         \
+  ( ((c) >= 'A' && (c) <= 'Z') ||               \
+    ((c) >= 'a' && (c) <= 'z') ||               \
+    ((c) == '_') || ((c) == '-'))
+
+/**
+ * Determine wether the given character is valid as a second or later
+ * character in a bus name
+ */
+#define VALID_BUS_NAME_CHARACTER(c)                 \
+  ( ((c) >= '0' && (c) <= '9') ||               \
+    ((c) >= 'A' && (c) <= 'Z') ||               \
+    ((c) >= 'a' && (c) <= 'z') ||               \
+    ((c) == '_') || ((c) == '-'))
+
+/**
+ * Checks that the given range of the string is a valid bus name in
+ * the D-BUS protocol. This includes a length restriction, etc., see
+ * the specification.
+ *
+ * @todo this is inconsistent with most of DBusString in that
+ * it allows a start,len range that extends past the string end.
+ *
+ * @param str the string
+ * @param start first byte index to check
+ * @param len number of bytes to check
+ * @returns #TRUE if the byte range exists and is a valid name
+ */
+dbus_bool_t
+_dbus_validate_bus_name (const DBusString  *str,
+                         int                start,
+                         int                len)
 {
   const unsigned char *s;
   const unsigned char *end;
-  const unsigned char *name;
+  const unsigned char *iface;
+  const unsigned char *last_dot;
 
   _dbus_assert (start >= 0);
   _dbus_assert (len >= 0);
@@ -942,12 +974,47 @@
   if (len > DBUS_MAXIMUM_NAME_LENGTH)
     return FALSE;
 
-  _dbus_assert (len > 0);
+  if (len == 0)
+    return FALSE;
 
-  name = _dbus_string_get_const_data (str) + start;
-  end = name + len;
-  _dbus_assert (*name == ':');
-  s = name + 1;
+  last_dot = NULL;
+  iface = _dbus_string_get_const_data (str) + start;
+  end = iface + len;
+  s = iface;
+
+  /* check special cases of first char so it doesn't have to be done
+   * in the loop. Note we know len > 0
+   */
+  if (*s == ':')
+  {
+    /* unique name */
+    ++s;
+    while (s != end)
+      {
+        if (*s == '.')
+          {
+            if (_DBUS_UNLIKELY ((s + 1) == end))
+              return FALSE;
+            if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*(s + 1))))
+              return FALSE;
+            ++s; /* we just validated the next char, so skip two */
+          }
+        else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
+          {
+            return FALSE;
+          }
+
+        ++s;
+      }
+
+    return TRUE;
+  }
+  else if (_DBUS_UNLIKELY (*s == '.')) /* disallow starting with a . */
+    return FALSE;
+  else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*s)))
+    return FALSE;
+  else
+    ++s;
 
   while (s != end)
     {
@@ -955,11 +1022,12 @@
         {
           if (_DBUS_UNLIKELY ((s + 1) == end))
             return FALSE;
-          if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*(s + 1))))
+          else if (_DBUS_UNLIKELY (!VALID_INITIAL_BUS_NAME_CHARACTER (*(s + 1))))
             return FALSE;
+          last_dot = s;
           ++s; /* we just validated the next char, so skip two */
         }
-      else if (_DBUS_UNLIKELY (!VALID_NAME_CHARACTER (*s)))
+      else if (_DBUS_UNLIKELY (!VALID_BUS_NAME_CHARACTER (*s)))
         {
           return FALSE;
         }
@@ -967,33 +1035,10 @@
       ++s;
     }
 
-  return TRUE;
-}
-
-/**
- * Checks that the given range of the string is a valid bus name in
- * the D-BUS protocol. This includes a length restriction, etc., see
- * the specification.
- *
- * @todo this is inconsistent with most of DBusString in that
- * it allows a start,len range that extends past the string end.
- *
- * @param str the string
- * @param start first byte index to check
- * @param len number of bytes to check
- * @returns #TRUE if the byte range exists and is a valid name
- */
-dbus_bool_t
-_dbus_validate_bus_name (const DBusString  *str,
-                         int                start,
-                         int                len)
-{
-  if (_DBUS_UNLIKELY (len == 0))
+  if (_DBUS_UNLIKELY (last_dot == NULL))
     return FALSE;
-  if (_dbus_string_get_byte (str, start) == ':')
-    return _dbus_validate_unique_name (str, start, len);
-  else
-    return _dbus_validate_interface (str, start, len);
+
+  return TRUE;
 }
 
 /**



More information about the dbus-commit mailing list