chiark / gitweb /
sd-device: ensure update_properties_buf() is a noop on failure
authorTom Gundersen <teg@jklm.no>
Mon, 1 Jun 2015 14:28:58 +0000 (16:28 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 09:01:41 +0000 (10:01 +0100)
Don't clobber the sd_device struct, and don't leak memory when memory allocation fails.

src/libelogind/sd-device/device-private.c

index deb8efd05dc75f9fb099735070726f284c508cfd..2e604332467d048e8c182ce6f1900b37f433c7e2 100644 (file)
@@ -636,9 +636,10 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
 
 static int device_update_properties_bufs(sd_device *device) {
         const char *val, *prop;
-        uint8_t *buf_nulstr = NULL;
+        _cleanup_free_ char **buf_strv = NULL;
+        _cleanup_free_ uint8_t *buf_nulstr = NULL;
         size_t allocated_nulstr = 0;
-        size_t nulstr_len = 0, num = 0, i;
+        size_t nulstr_len = 0, num = 0, i = 0;
 
         assert(device);
 
@@ -659,20 +660,25 @@ static int device_update_properties_bufs(sd_device *device) {
                 ++num;
         }
 
-        free(device->properties_nulstr);
-        device->properties_nulstr = buf_nulstr;
-        device->properties_nulstr_len = nulstr_len;
+        /* build buf_strv from buf_nulstr */
+        buf_strv = new0(char *, num + 1);
+        if (!buf_strv)
+                return -ENOMEM;
 
-        /* build strv from buf_nulstr */
-        free(device->properties_strv);
-        device->properties_strv = new0(char *, num + 1);
-        i = 0;
         NULSTR_FOREACH(val, (char*) buf_nulstr) {
-                device->properties_strv[i] = (char *) val;
+                buf_strv[i] = (char *) val;
                 assert(i < num);
                 i++;
         }
 
+        free(device->properties_nulstr);
+        device->properties_nulstr = buf_nulstr;
+        buf_nulstr = NULL;
+        device->properties_nulstr_len = nulstr_len;
+        free(device->properties_strv);
+        device->properties_strv = buf_strv;
+        buf_strv = NULL;
+
         device->properties_buf_outdated = false;
 
         return 0;