#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"
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;
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))