]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
DBusString: extend with checking for starting with words
authorAdrian Szyndela <adrian.s@samsung.com>
Wed, 16 Jan 2019 11:30:22 +0000 (12:30 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Fri, 26 Apr 2019 11:29:42 +0000 (13:29 +0200)
This extracts a few lines of code and adds it as a DBusString function
that checks if a DBusString starts with words given with a C string
and a word separator. In other words, it checks if:
- a DBusString is a given C string, or
- a DBusString starts with a given C string and the next character is
  a given word separator.

It is used for matching names to prefixes when checking the policy.

Signed-off-by: Adrian Szyndela <adrian.s@samsung.com>
Change-Id: Ie39d33916863d950dde38d3b8b20c8a539217302

bus/policy.c
dbus/dbus-string.c
dbus/dbus-string.h

index c87bfecffd6d4b1f9252a1cea9d369e505fecbed..11d21bf01467f3882dd886b51072314ed6bfb042 100644 (file)
@@ -1341,15 +1341,9 @@ bus_rules_check_can_own (DBusList *rules,
         }
       else if (rule->d.own.prefix)
         {
-          const char *data;
-          char next_char;
-          if (!_dbus_string_starts_with_c_str (service_name,
-                                               rule->d.own.service_name))
-            continue;
-
-          data = _dbus_string_get_const_data (service_name);
-          next_char = data[strlen (rule->d.own.service_name)];
-          if (next_char != '\0' && next_char != '.')
+          if (!_dbus_string_starts_with_words_c_str (service_name,
+                                                     rule->d.own.service_name,
+                                                     '.'))
             continue;
         }
 
index 52c71c5b08c54b4562ed77c86ba56f8f5ad78e7e..d620067e19ee900c2b6975a17a3ddf9d6a0c68f8 100644 (file)
@@ -2237,6 +2237,32 @@ _dbus_string_starts_with_c_str (const DBusString *a,
     return FALSE;
 }
 
+/**
+ * Checks whether a string starts with the given C string, after which it ends or is separated from
+ * the rest by a given separator character.
+ *
+ * @param a the string
+ * @param c_str the C string
+ * @param word_separator the separator
+ * @returns #TRUE if string starts with it
+ */
+dbus_bool_t
+_dbus_string_starts_with_words_c_str (const DBusString  *a,
+                                      const char        *c_str,
+                                      char               word_separator)
+{
+  char next_char;
+  const char *data;
+  _dbus_assert (c_str != NULL);
+
+  if (!_dbus_string_starts_with_c_str (a, c_str))
+    return FALSE;
+
+  data = _dbus_string_get_const_data (a);
+  next_char = data[strlen (c_str)];
+  return next_char == '\0' || next_char == word_separator;
+}
+
 /**
  * Appends a two-character hex digit to a string, where the hex digit
  * has the value of the given byte.
index 41e82ab9e6b9e3d34f4eb0c92943c3c36f043535..7ac32d38ebfe57d37e4506c48a3b6a7a0b7bcacc 100644 (file)
@@ -338,6 +338,10 @@ dbus_bool_t   _dbus_string_starts_with_c_str     (const DBusString  *a,
 dbus_bool_t   _dbus_string_ends_with_c_str       (const DBusString  *a,
                                                   const char        *c_str);
 DBUS_PRIVATE_EXPORT
+dbus_bool_t   _dbus_string_starts_with_words_c_str (const DBusString  *a,
+                                                    const char        *c_str,
+                                                    char               word_separator);
+DBUS_PRIVATE_EXPORT
 dbus_bool_t   _dbus_string_pop_line              (DBusString        *source,
                                                   DBusString        *dest);
 DBUS_PRIVATE_EXPORT