From 25108fe4a49e649cb0b3053a47249977a732b5b9 Mon Sep 17 00:00:00 2001 From: Michal Suchanek Date: Sun, 7 Jan 2024 15:55:48 +0100 Subject: [PATCH] More robust reading of labels When the sysfs file is copied to a plain file it may contain trailing zeroes which is not handled well by the label reading code. --- lib/access.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/access.c b/lib/access.c index 149ed712..96541adb 100644 --- a/lib/access.c +++ b/lib/access.c @@ -168,7 +168,7 @@ char *sensors_get_label(const sensors_chip_name *name, { char *label; const sensors_chip *chip; - char buf[PATH_MAX]; + char buf[PATH_MAX + 1] = {0}; FILE *f; int i; @@ -183,12 +183,14 @@ char *sensors_get_label(const sensors_chip_name *name, } /* No user specified label, check for a _label sysfs file */ - snprintf(buf, PATH_MAX, "%s/%s_label", name->path, feature->name); - + snprintf(buf, sizeof(buf) - 1, "%s/%s_label", name->path, feature->name); + if ((f = fopen(buf, "r"))) { - i = fread(buf, 1, sizeof(buf), f); + i = fread(buf, 1, sizeof(buf) - 1, f); fclose(f); if (i > 0) { + buf[i] = 0; /* terminate string */ + i = strlen(buf); /* strip trailing zeroes */ /* i - 1 to strip the '\n' at the end */ buf[i - 1] = 0; label = buf;