The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc
@ 2025-12-15 12:20 Thorsten Blum
  2026-01-08 12:29 ` Johannes Berg
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2025-12-15 12:20 UTC (permalink / raw)
  To: Johannes Berg; +Cc: Thorsten Blum, linux-wireless, linux-kernel

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc
  2025-12-15 12:20 [PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc Thorsten Blum
@ 2026-01-08 12:29 ` Johannes Berg
  2026-02-20 17:00   ` Thorsten Blum
  0 siblings, 1 reply; 3+ messages in thread
From: Johannes Berg @ 2026-01-08 12:29 UTC (permalink / raw)
  To: Thorsten Blum; +Cc: linux-wireless, linux-kernel

On Mon, 2025-12-15 at 13:20 +0100, Thorsten Blum wrote:
> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc
  2026-01-08 12:29 ` Johannes Berg
@ 2026-02-20 17:00   ` Thorsten Blum
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-02-20 17:00 UTC (permalink / raw)
  To: Johannes Berg; +Cc: linux-wireless, linux-kernel

On 8. Jan 2026, at 13:29, Johannes Berg wrote:
> On Mon, 2025-12-15 at 13:20 +0100, Thorsten Blum wrote:
>> 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"...

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

Thanks,
Thorsten


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-02-20 17:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-15 12:20 [PATCH net-next] net: rfkill: Replace strcpy with memcpy to improve rfkill_alloc Thorsten Blum
2026-01-08 12:29 ` Johannes Berg
2026-02-20 17:00   ` Thorsten Blum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox