chiark / gitweb /
sd-bus: avoid potential memory leaks
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 9 Feb 2018 07:21:29 +0000 (16:21 +0900)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:53:59 +0000 (07:53 +0200)
(cherry picked from commit b4ca3f45dc5742ad76e8feebd363c490f92b804f)

src/libelogind/sd-bus/sd-bus.c

index 505ce1337a370b0433d2fdf2c0e3ac8d10fe169b..f11cdce850b8d73cd7c8751b826cd49094900c7e 100644 (file)
@@ -561,6 +561,7 @@ void bus_set_state(sd_bus *bus, enum bus_state state) {
 
 static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
         const char *s;
+        char *t;
         sd_bus *bus;
         int r;
 
@@ -580,10 +581,12 @@ static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *e
         if (!service_name_is_valid(s) || s[0] != ':')
                 return -EBADMSG;
 
-        bus->unique_name = strdup(s);
-        if (!bus->unique_name)
+        t = strdup(s);
+        if (!t)
                 return -ENOMEM;
 
+        free_and_replace(bus->unique_name, t);
+
         if (bus->state == BUS_HELLO) {
                 bus_set_state(bus, BUS_RUNNING);
 
@@ -1392,7 +1395,7 @@ fail:
 
 int bus_set_address_system_remote(sd_bus *b, const char *host) {
         _cleanup_free_ char *e = NULL;
-        char *m = NULL, *c = NULL;
+        char *m = NULL, *c = NULL, *a;
 
         assert(b);
         assert(host);
@@ -1423,10 +1426,12 @@ int bus_set_address_system_remote(sd_bus *b, const char *host) {
                         return -ENOMEM;
         }
 
-        b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=systemd-stdio-bridge", c);
-        if (!b->address)
+        a = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=elogind-stdio-bridge", c);
+        if (!a)
                 return -ENOMEM;
 
+        free_and_replace(b->address, a);
+
         return 0;
  }
 
@@ -1464,6 +1469,7 @@ fail:
 
 int bus_set_address_system_machine(sd_bus *b, const char *machine) {
         _cleanup_free_ char *e = NULL;
+        char *a;
 
         assert(b);
         assert(machine);
@@ -1472,10 +1478,12 @@ int bus_set_address_system_machine(sd_bus *b, const char *machine) {
         if (!e)
                 return -ENOMEM;
 
-        b->address = strjoin("x-machine-unix:machine=", e);
-        if (!b->address)
+        a = strjoin("x-machine-unix:machine=", e);
+        if (!a)
                 return -ENOMEM;
 
+        free_and_replace(b->address, a);
+
         return 0;
 }