diff options
author | Tom Gundersen <teg@jklm.no> | 2015-06-01 16:28:58 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-06-01 16:28:58 +0200 |
commit | ccc1002a1c510b7d4631833eaf60225f028f2280 (patch) | |
tree | bbe5e21d30d164c3fc2f2c2a5cde08092d323d59 | |
parent | c521a430fd6027d55f96516bc2f7570f5997e137 (diff) |
sd-device: ensure update_properties_buf() is a noop on failure
Don't clobber the sd_device struct, and don't leak memory when memory allocation fails.
-rw-r--r-- | src/libsystemd/sd-device/device-private.c | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c index deb8efd05d..2e60433246 100644 --- a/src/libsystemd/sd-device/device-private.c +++ b/src/libsystemd/sd-device/device-private.c @@ -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; |