[PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc

Thorsten Blum posted 1 patch 5 hours ago
net/rfkill/core.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc
Posted by Thorsten Blum 5 hours ago
strcpy() is deprecated [1] and uses an additional strlen() internally;
use memcpy() directly since we already know the length of 'name' and
that it is guaranteed to be NUL-terminated.

Use struct_size(), which provides additional compile-time checks for
structures with flexible array members (e.g., __must_be_array()), to
determine the allocation size for a new 'struct rfkill'.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 net/rfkill/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/net/rfkill/core.c b/net/rfkill/core.c
index 7d3e82e4c2fc..db1260acb182 100644
--- a/net/rfkill/core.c
+++ b/net/rfkill/core.c
@@ -986,6 +986,7 @@ struct rfkill * __must_check rfkill_alloc(const char *name,
 {
 	struct rfkill *rfkill;
 	struct device *dev;
+	size_t name_sz;
 
 	if (WARN_ON(!ops))
 		return NULL;
@@ -999,14 +1000,15 @@ struct rfkill * __must_check rfkill_alloc(const char *name,
 	if (WARN_ON(type == RFKILL_TYPE_ALL || type >= NUM_RFKILL_TYPES))
 		return NULL;
 
-	rfkill = kzalloc(sizeof(*rfkill) + strlen(name) + 1, GFP_KERNEL);
+	name_sz = strlen(name) + 1;
+	rfkill = kzalloc(struct_size(rfkill, name, name_sz), GFP_KERNEL);
 	if (!rfkill)
 		return NULL;
 
 	spin_lock_init(&rfkill->lock);
 	INIT_LIST_HEAD(&rfkill->node);
 	rfkill->type = type;
-	strcpy(rfkill->name, name);
+	memcpy(rfkill->name, name, name_sz);
 	rfkill->ops = ops;
 	rfkill->data = ops_data;
 
-- 
Thorsten Blum <thorsten.blum@linux.dev>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4