Skip to contentSkip to conversation

hwrng: core - Replace strlcat() with better alternative

PATCH In review Andy Shevchenko 3 participants 4 messages May 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwrng: core - Replace strlcat() with better alternative

strlcpy() and strlcat() are confusing APIs and the former one already gone from the kernel.

In preparation to kill strlcat() replace it with the better alternative.

---
 drivers/char/hw_random/core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/char/hw_random/core.c +7 -7
index aba92d777f72..c789699bd773 100644
@@ -418,21 +418,21 @@ static ssize_t rng_available_show(struct device *dev,
 				  struct device_attribute *attr,
 				  char *buf)
 {
+	int len = 0;
 	int err;
 	struct hwrng *rng;
 
 	err = mutex_lock_interruptible(&rng_mutex);
 	if (err)
 		return -ERESTARTSYS;
-	buf[0] = '\0';
-	list_for_each_entry(rng, &rng_list, list) {
-		strlcat(buf, rng->name, PAGE_SIZE);
-		strlcat(buf, " ", PAGE_SIZE);
-	}
-	strlcat(buf, "none\n", PAGE_SIZE);
+
+	list_for_each_entry(rng, &rng_list, list)
+		len += sysfs_emit_at(buf, len, "%s ", rng->name);
+	len += sysfs_emit_at(buf, len, "none\n");
+
 	mutex_unlock(&rng_mutex);
 
-	return strlen(buf);
+	return len;
 }
 
 static ssize_t rng_selected_show(struct device *dev,
Signature · 1 line
2.50.1
#2 lore raw
Quoted 11 lines · Thorsten Blum
Quoted 6 lines · Andy Shevchenko

strlcpy() and strlcat() are confusing APIs and the former one already gone from the kernel.

In preparation to kill strlcat() replace it with the better alternative.

I already took care of this one a few days ago:

https://lore.kernel.org/lkml/[email protected]/

Very good, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v1 1/1
#3 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · On Mon, 4 May 2026 15:02:59 +0200 Andy Shevchenko…

strlcpy() and strlcat() are confusing APIs and the former one already gone from the kernel.

In preparation to kill strlcat() replace it with the better alternative.

Quoted 37 lines
---
 drivers/char/hw_random/core.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/char/hw_random/core.c +7 -7
index aba92d777f72..c789699bd773 100644
@@ -418,21 +418,21 @@ static ssize_t rng_available_show(struct device *dev,
 				  struct device_attribute *attr,
 				  char *buf)
 {
+	int len = 0;
 	int err;
 	struct hwrng *rng;
 
 	err = mutex_lock_interruptible(&rng_mutex);
 	if (err)
 		return -ERESTARTSYS;
-	buf[0] = '\0';
-	list_for_each_entry(rng, &rng_list, list) {
-		strlcat(buf, rng->name, PAGE_SIZE);
-		strlcat(buf, " ", PAGE_SIZE);
-	}
-	strlcat(buf, "none\n", PAGE_SIZE);
+
+	list_for_each_entry(rng, &rng_list, list)
+		len += sysfs_emit_at(buf, len, "%s ", rng->name);
+	len += sysfs_emit_at(buf, len, "none\n");
+
 	mutex_unlock(&rng_mutex);
 
-	return strlen(buf);
+	return len;
 }
 
 static ssize_t rng_selected_show(struct device *dev,