refactoring: use string_ref for extracting bus_owner 45/202545/4
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 29 Mar 2019 14:15:41 +0000 (15:15 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Tue, 2 Apr 2019 10:38:32 +0000 (12:38 +0200)
Change-Id: I4009d4bf06ba09c3e8177c8ffbb0dec6a1d05000

src/libdbuspolicy1.cpp

index 87ec326..66c8950 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/limits.h>
 #include <assert.h>
 #include <mutex>
+#include <boost/utility/string_ref.hpp>
 
 #define KDBUS_PATH_PREFIX "/sys/fs/kdbus/"
 #define KDBUS_SYSTEM_BUS_PATH "/sys/fs/kdbus/0-system/bus"
@@ -107,9 +108,6 @@ static bool bus_type_from_path(const char *bus_path, BusType *bus_type, char **p
 
 static int bus_path_resolve(const char *bus_path, char *resolved_path, unsigned resolved_path_size, BusType *bus_type, uid_t *bus_owner)
 {
-       const char user_suffix[] = "-user/bus";
-       int suffix_pos;
-       char* last_slash;
        int ret = -1;
        char *p;
 
@@ -117,22 +115,22 @@ static int bus_path_resolve(const char *bus_path, char *resolved_path, unsigned
                return -1;
 
        if (*bus_type == SESSION_BUS && NULL != bus_owner) {
-               suffix_pos = strlen(p) - strlen(user_suffix);
-               if (suffix_pos < 0)
-                       goto err;
+               boost::string_ref user_suffix("-user/bus");
+               boost::string_ref ps(p);
 
-               if (0 != strcmp(p + suffix_pos, user_suffix))
+               if (!ps.ends_with(user_suffix))
                        goto err;
 
-               last_slash = static_cast<char*>(memrchr(p, '/', suffix_pos));
-               if (last_slash == NULL)
-                       goto err;
-               last_slash++;
-               if ((int)(last_slash - p) > (int)strlen(p))
+               ps.remove_suffix(user_suffix.length());
+
+               size_t last_slash = ps.find_last_of('/');
+               if (last_slash == boost::string_ref::npos || last_slash == ps.length() - 1)
                        goto err;
 
+               ps = ps.substr(last_slash+1);
+
                errno = 0;
-               *bus_owner = strtol(last_slash, NULL, 10);
+               *bus_owner = strtol(ps.data(), NULL, 10);
                // We will never get 0 as a session bus owner (0 uses "0-system" bus),
                // so it's OK to assume that *bus_owner==0 always means error.
                if (errno || (*bus_owner == 0))