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.
This commit is contained in:
Michal Suchanek committed 2024-01-07 21:20:39 +01:00
1 parent 3bbd975860
commit 25108fe4a4
1 file changed
+6 -4
+6 -4
View File
@@ -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;