chiark / gitweb /
sd-bus: don't clobber return values on failure in bus_kernel_open_bus_fd()
authorLennart Poettering <lennart@poettering.net>
Wed, 26 Nov 2014 01:54:11 +0000 (02:54 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 26 Nov 2014 02:06:50 +0000 (03:06 +0100)
src/libsystemd/sd-bus/bus-kernel.c

index aa4b424..6baa84c 100644 (file)
@@ -1514,20 +1514,29 @@ int bus_kernel_open_bus_fd(const char *bus, char **path) {
         int fd;
         size_t len;
 
+        assert(bus);
+
         len = strlen("/sys/fs/kdbus/") + DECIMAL_STR_MAX(uid_t) + 1 + strlen(bus) + strlen("/bus") + 1;
 
         if (path) {
-                p = malloc(len);
+                p = new(char, len);
                 if (!p)
                         return -ENOMEM;
-                *path = p;
         } else
-                p = alloca(len);
+                p = newa(char, len);
+
         sprintf(p, "/sys/fs/kdbus/" UID_FMT "-%s/bus", getuid(), bus);
 
         fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC);
-        if (fd < 0)
+        if (fd < 0) {
+                if (path)
+                        free(p);
+
                 return -errno;
+        }
+
+        if (path)
+                *path = p;
 
         return fd;
 }