mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
dev: be safer when reading sysfs properties
Check if the value we read from sysfs is not blank and replace the '\n' at the end only when needed ('\n' should usually be there for sysfs values, but better check this).
This commit is contained in:
parent
ca7bac53cf
commit
8fad9b9e5d
@ -365,6 +365,7 @@ static int _add_alias(struct device *dev, const char *path)
|
||||
static int _get_sysfs_value(const char *path, char *buf, size_t buf_size)
|
||||
{
|
||||
FILE *fp;
|
||||
size_t len;
|
||||
|
||||
if (!(fp = fopen(path, "r"))) {
|
||||
log_sys_error("fopen", path);
|
||||
@ -378,7 +379,13 @@ static int _get_sysfs_value(const char *path, char *buf, size_t buf_size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf[strlen(buf) - 1] = '\0';
|
||||
if (!(len = strlen(buf))) {
|
||||
log_error("_get_sysfs_value: %s: no value", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
|
||||
if (fclose(fp))
|
||||
log_sys_error("fclose", path);
|
||||
|
Loading…
Reference in New Issue
Block a user