From a885b93c3c545e8c32c13c1f304c3839bc466fbd Mon Sep 17 00:00:00 2001 From: illiliti Date: Fri, 21 May 2021 05:31:53 +0300 Subject: [PATCH] udev_device.c: correctly handle multiline sysattrs Changed arbitrary size of sysattr to PAGESIZE[0]. Not sure about binary sysattrs because they may be > PAGESIZE. Either way, we need dynamic allocation to handle them. Trim only trailing newlines, not the first one. Now content of the sysattr not getting truncated. By the way, do we need to trim carrige return '\r' too? [0] https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt > The buffer will always be PAGE_SIZE bytes in length. On i386, this is 4096. Fixes #25 --- udev_device.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/udev_device.c b/udev_device.c index 92c1a41..b487fd3 100644 --- a/udev_device.c +++ b/udev_device.c @@ -212,11 +212,10 @@ const char *udev_device_get_property_value(struct udev_device *udev_device, cons const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr) { struct udev_list_entry *list_entry; - char data[1024], path[PATH_MAX]; + char data[PAGESIZE], path[PATH_MAX]; struct stat st; size_t len; FILE *file; - char *pos; if (!udev_device || !sysattr) { return NULL; @@ -240,19 +239,21 @@ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const return NULL; } - len = fread(data, 1, sizeof(data), file); + // TODO dynamic allocation of data + len = fread(data, 1, sizeof(data)-1, file); - if (len != sizeof(data) && ferror(file)) { + if (len != sizeof(data)-1 && ferror(file)) { fclose(file); return NULL; } - if ((pos = memchr(data, '\n', len))) { - len = pos - data; - } - fclose(file); data[len] = '\0'; + + while (len-- > 0 && data[len] == '\n') { + data[len] = '\0'; + } + list_entry = udev_list_entry_add(&udev_device->sysattrs, sysattr, data, 0); return udev_list_entry_get_value(list_entry); }