]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
hwrng: core - use sysfs_emit_at in rng_available_show
authorThorsten Blum <thorsten.blum@linux.dev>
Tue, 5 May 2026 09:45:58 +0000 (11:45 +0200)
committerHerbert Xu <herbert@gondor.apana.org.au>
Fri, 15 May 2026 10:08:37 +0000 (18:08 +0800)
Replace strlcat() with sysfs_emit_at() in rng_available_show() and add
'int len' to keep track of the number of bytes written. sysfs_emit_at()
is preferred for formatting sysfs output because it provides safer
bounds checking.

Inline mutex_lock_interruptible() and drop the now-unused local error
variable. Remove the unnecessary 'buf' NUL initialization. Return 'len'
directly instead of strlen(buf).

Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
drivers/char/hw_random/core.c

index 26c46cd90a83c67a36c9b53507a166bc0508a3d0..6931657ad2caa8f6b30792786cb76cc2eab95769 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/sched/signal.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 #include <linux/uaccess.h>
 #include <linux/workqueue.h>
 
@@ -414,21 +415,17 @@ static ssize_t rng_available_show(struct device *dev,
                                  struct device_attribute *attr,
                                  char *buf)
 {
-       int err;
        struct hwrng *rng;
+       int len = 0;
 
-       err = mutex_lock_interruptible(&rng_mutex);
-       if (err)
+       if (mutex_lock_interruptible(&rng_mutex))
                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,