[PATCH 4/5] hwmon: Reject truncated attribute names
From: Jihong Min
Date: Fri Oct 09 2026 - 11:52:52 EST
Large channel numbers can produce truncated or conflicting attribute
names. Return -EOVERFLOW if a generated name does not fit its buffer.
Assisted-by: Codex:gpt-6-astra
Signed-off-by: Jihong Min <hurryman2212@xxxxxxxxx>
---
drivers/hwmon/hwmon.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index bed16cfd4f1c..288f356980e4 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -553,8 +553,12 @@ static const struct attribute *hwmon_genattr(const void *drvdata,
if (type == hwmon_chip) {
name = template;
} else {
- scnprintf(hattr->name, sizeof(hattr->name), template,
- index + hwmon_attr_base(type));
+ if (snprintf(hattr->name, sizeof(hattr->name), template,
+ index + hwmon_attr_base(type)) >=
+ sizeof(hattr->name)) {
+ kfree(hattr);
+ return ERR_PTR(-EOVERFLOW);
+ }
name = hattr->name;
}