Skip to contentSkip to conversation

net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc

PATCH In review Thorsten Blum 2 participants 3 messages Dec 15, 2025 – Feb 20 Rfkill

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc

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'.

---
 net/rfkill/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/rfkill/core.c +4 -2
index 7d3e82e4c2fc..db1260acb182 100644
@@ -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;
 
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
maintainer
#1 lore raw
Quoted 7 lines · Thorsten Blum

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'.

TBH, I don't really see that this is a real _improvement_. I guess I'll take it if you sell it as "let's not use deprecated strcpy" instead, although even the documentation says "no new uses"...

johannes

#2 lore raw
Quoted 5 lines · Johannes Berg
Quoted 4 lines · Thorsten Blum

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.

Quoted 7 lines
Quoted 3 lines

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'.

TBH, I don't really see that this is a real _improvement_. I guess I'll take it if you sell it as "let's not use deprecated strcpy" instead, although even the documentation says "no new uses"...

Yes, this is primarily a refactoring to avoid deprecated strcpy(), and to harden the code by using struct_size().

Thanks,
Thorsten