chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
sd-device: get_sysattr_value - don't leak returned value
[elogind.git]
/
src
/
libsystemd
/
sd-device
/
sd-device.c
diff --git
a/src/libsystemd/sd-device/sd-device.c
b/src/libsystemd/sd-device/sd-device.c
index 3179d16b213a45a5eb0308f644b90dceed4cbc03..0dd0e14eef929283b67345cb201e0c3b5b72f23b 100644
(file)
--- a/
src/libsystemd/sd-device/sd-device.c
+++ b/
src/libsystemd/sd-device/sd-device.c
@@
-1593,9
+1593,9
@@
_public_ int sd_device_get_property_value(sd_device *device, const char *key, co
}
/* replaces the value if it already exists */
}
/* replaces the value if it already exists */
-static int device_add_sysattr_value(sd_device *device, const char *_key, c
onst char *_
value) {
+static int device_add_sysattr_value(sd_device *device, const char *_key, c
har *
value) {
_cleanup_free_ char *key = NULL;
_cleanup_free_ char *key = NULL;
- _cleanup_free_ char *value = NULL;
+ _cleanup_free_ char *value
_old
= NULL;
int r;
assert(device);
int r;
assert(device);
@@
-1605,28
+1605,18
@@
static int device_add_sysattr_value(sd_device *device, const char *_key, const c
if (r < 0)
return r;
if (r < 0)
return r;
- value = hashmap_remove2(device->sysattr_values, _key, (void **)&key);
+ value
_old
= hashmap_remove2(device->sysattr_values, _key, (void **)&key);
if (!key) {
key = strdup(_key);
if (!key)
return -ENOMEM;
}
if (!key) {
key = strdup(_key);
if (!key)
return -ENOMEM;
}
- free(value);
- value = NULL;
-
- if (_value) {
- value = strdup(_value);
- if (!value)
- return -ENOMEM;
- }
-
r = hashmap_put(device->sysattr_values, key, value);
if (r < 0)
return r;
key = NULL;
r = hashmap_put(device->sysattr_values, key, value);
if (r < 0)
return r;
key = NULL;
- value = NULL;
return 0;
}
return 0;
}
@@
-1740,8
+1730,9
@@
static void device_remove_sysattr_value(sd_device *device, const char *_key) {
/* set the attribute and save it in the cache. If a NULL value is passed the
* attribute is cleared from the cache */
/* set the attribute and save it in the cache. If a NULL value is passed the
* attribute is cleared from the cache */
-_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *value) {
+_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, char *
_
value) {
_cleanup_close_ int fd = -1;
_cleanup_close_ int fd = -1;
+ _cleanup_free_ char *value = NULL;
const char *syspath;
char *path;
struct stat statbuf;
const char *syspath;
char *path;
struct stat statbuf;
@@
-1752,7
+1743,7
@@
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
assert_return(device, -EINVAL);
assert_return(sysattr, -EINVAL);
- if (!value) {
+ if (!
_
value) {
device_remove_sysattr_value(device, sysattr);
return 0;
device_remove_sysattr_value(device, sysattr);
return 0;
@@
-1765,7
+1756,11
@@
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
path = strjoina(syspath, "/", sysattr);
r = lstat(path, &statbuf);
if (r < 0) {
path = strjoina(syspath, "/", sysattr);
r = lstat(path, &statbuf);
if (r < 0) {
- r = device_add_sysattr_value(device, sysattr, "");
+ value = strdup("");
+ if (!value)
+ return -ENOMEM;
+
+ r = device_add_sysattr_value(device, sysattr, value);
if (r < 0)
return r;
if (r < 0)
return r;
@@
-1783,11
+1778,11
@@
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
if ((statbuf.st_mode & S_IRUSR) == 0)
return -EACCES;
if ((statbuf.st_mode & S_IRUSR) == 0)
return -EACCES;
- value_len = strlen(value);
+ value_len = strlen(
_
value);
/* drop trailing newlines */
/* drop trailing newlines */
- while (value_len > 0 && value[--value_len] == '\n')
- value[value_len] = '\0';
+ while (value_len > 0 &&
_
value[--value_len] == '\n')
+
_
value[value_len] = '\0';
/* value length is limited to 4k */
if (value_len > 4096)
/* value length is limited to 4k */
if (value_len > 4096)
@@
-1797,6
+1792,10
@@
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
if (fd < 0)
return -errno;
if (fd < 0)
return -errno;
+ value = strdup(_value);
+ if (!value)
+ return -ENOMEM;
+
size = write(fd, value, value_len);
if (size < 0)
return -errno;
size = write(fd, value, value_len);
if (size < 0)
return -errno;
@@
-1808,5
+1807,7
@@
_public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
if (r < 0)
return r;
if (r < 0)
return r;
+ value = NULL;
+
return 0;
}
return 0;
}