netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] net: ipconfig: replace strncpy with strscpy
@ 2025-04-12 16:06 Pranav Tyagi
  2025-04-15 16:35 ` Simon Horman
  0 siblings, 1 reply; 5+ messages in thread
From: Pranav Tyagi @ 2025-04-12 16:06 UTC (permalink / raw)
  To: davem, dsahern, edumazet, kuba, pabeni, horms, skhan, netdev,
	linux-kernel, linux-kernel-mentees
  Cc: Pranav Tyagi

Replace the deprecated strncpy() with strscpy() as the destination
buffer is NUL-terminated and does not require any
trailing NUL-padding.

Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>
---
 net/ipv4/ipconfig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/ipv4/ipconfig.c b/net/ipv4/ipconfig.c
index c56b6fe6f0d7..eb9b32214e60 100644
--- a/net/ipv4/ipconfig.c
+++ b/net/ipv4/ipconfig.c
@@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
 			*v = 0;
 			if (kstrtou8(client_id, 0, dhcp_client_identifier))
 				pr_debug("DHCP: Invalid client identifier type\n");
-			strncpy(dhcp_client_identifier + 1, v + 1, 251);
+			strscpy(dhcp_client_identifier + 1, v + 1, 251);
 			*v = ',';
 		}
 		return 1;
-- 
2.49.0


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

* Re: [PATCH net-next] net: ipconfig: replace strncpy with strscpy
  2025-04-12 16:06 [PATCH net-next] net: ipconfig: replace strncpy with strscpy Pranav Tyagi
@ 2025-04-15 16:35 ` Simon Horman
  2025-04-16  0:19   ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Horman @ 2025-04-15 16:35 UTC (permalink / raw)
  To: Pranav Tyagi
  Cc: davem, dsahern, edumazet, kuba, pabeni, skhan, netdev,
	linux-kernel, linux-kernel-mentees

On Sat, Apr 12, 2025 at 09:36:23PM +0530, Pranav Tyagi wrote:
> Replace the deprecated strncpy() with strscpy() as the destination
> buffer is NUL-terminated and does not require any
> trailing NUL-padding.
> 
> Signed-off-by: Pranav Tyagi <pranav.tyagi03@gmail.com>

Thanks,

I agree that strscpy() is the correct choice here for
the reasons you give above.

Reviewed-by: Simon Horman <horms@kernel.org>

> ---
>  net/ipv4/ipconfig.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/ipv4/ipconfig.c b/net/ipv4/ipconfig.c
> index c56b6fe6f0d7..eb9b32214e60 100644
> --- a/net/ipv4/ipconfig.c
> +++ b/net/ipv4/ipconfig.c
> @@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
>  			*v = 0;
>  			if (kstrtou8(client_id, 0, dhcp_client_identifier))
>  				pr_debug("DHCP: Invalid client identifier type\n");
> -			strncpy(dhcp_client_identifier + 1, v + 1, 251);
> +			strscpy(dhcp_client_identifier + 1, v + 1, 251);

As an aside, I'm curious to know why the length is 251
rather than 252 (sizeof(dhcp_client_identifier) -1).
But that isn't strictly related to this patch.

>  			*v = ',';
>  		}
>  		return 1;
> -- 
> 2.49.0
> 

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

* Re: [PATCH net-next] net: ipconfig: replace strncpy with strscpy
  2025-04-15 16:35 ` Simon Horman
@ 2025-04-16  0:19   ` Jakub Kicinski
  2025-04-16 11:17     ` Simon Horman
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2025-04-16  0:19 UTC (permalink / raw)
  To: Simon Horman
  Cc: Pranav Tyagi, davem, dsahern, edumazet, pabeni, skhan, netdev,
	linux-kernel, linux-kernel-mentees

On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:
> > @@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
> >  			*v = 0;
> >  			if (kstrtou8(client_id, 0, dhcp_client_identifier))
> >  				pr_debug("DHCP: Invalid client identifier type\n");
> > -			strncpy(dhcp_client_identifier + 1, v + 1, 251);
> > +			strscpy(dhcp_client_identifier + 1, v + 1, 251);  
> 
> As an aside, I'm curious to know why the length is 251
> rather than 252 (sizeof(dhcp_client_identifier) -1).
> But that isn't strictly related to this patch.

Isn't this because strncpy() doesn't nul-terminate, and since this is a
static variable if we use len - 1 we guarantee that there will be a null
byte at the end? If we switch to strscpy we'll make the max string len
1 char shorter.

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

* Re: [PATCH net-next] net: ipconfig: replace strncpy with strscpy
  2025-04-16  0:19   ` Jakub Kicinski
@ 2025-04-16 11:17     ` Simon Horman
  2025-04-16 15:01       ` Pranav Tyagi
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Horman @ 2025-04-16 11:17 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: Pranav Tyagi, davem, dsahern, edumazet, pabeni, skhan, netdev,
	linux-kernel, linux-kernel-mentees

On Tue, Apr 15, 2025 at 05:19:27PM -0700, Jakub Kicinski wrote:
> On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:
> > > @@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
> > >  			*v = 0;
> > >  			if (kstrtou8(client_id, 0, dhcp_client_identifier))
> > >  				pr_debug("DHCP: Invalid client identifier type\n");
> > > -			strncpy(dhcp_client_identifier + 1, v + 1, 251);
> > > +			strscpy(dhcp_client_identifier + 1, v + 1, 251);  
> > 
> > As an aside, I'm curious to know why the length is 251
> > rather than 252 (sizeof(dhcp_client_identifier) -1).
> > But that isn't strictly related to this patch.
> 
> Isn't this because strncpy() doesn't nul-terminate, and since this is a
> static variable if we use len - 1 we guarantee that there will be a null
> byte at the end? If we switch to strscpy we'll make the max string len
> 1 char shorter.

Yes, that makes sense to me.
And so I think the patch should also increase 251 to 252.

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

* Re: [PATCH net-next] net: ipconfig: replace strncpy with strscpy
  2025-04-16 11:17     ` Simon Horman
@ 2025-04-16 15:01       ` Pranav Tyagi
  0 siblings, 0 replies; 5+ messages in thread
From: Pranav Tyagi @ 2025-04-16 15:01 UTC (permalink / raw)
  To: Simon Horman
  Cc: Jakub Kicinski, davem, dsahern, edumazet, pabeni, skhan, netdev,
	linux-kernel, linux-kernel-mentees

On Wed, Apr 16, 2025 at 4:47 PM Simon Horman <horms@kernel.org> wrote:
>
> On Tue, Apr 15, 2025 at 05:19:27PM -0700, Jakub Kicinski wrote:
> > On Tue, 15 Apr 2025 17:35:36 +0100 Simon Horman wrote:
> > > > @@ -1690,7 +1690,7 @@ static int __init ic_proto_name(char *name)
> > > >                   *v = 0;
> > > >                   if (kstrtou8(client_id, 0, dhcp_client_identifier))
> > > >                           pr_debug("DHCP: Invalid client identifier type\n");
> > > > -                 strncpy(dhcp_client_identifier + 1, v + 1, 251);
> > > > +                 strscpy(dhcp_client_identifier + 1, v + 1, 251);
> > >
> > > As an aside, I'm curious to know why the length is 251
> > > rather than 252 (sizeof(dhcp_client_identifier) -1).
> > > But that isn't strictly related to this patch.
> >
> > Isn't this because strncpy() doesn't nul-terminate, and since this is a
> > static variable if we use len - 1 we guarantee that there will be a null
> > byte at the end? If we switch to strscpy we'll make the max string len
> > 1 char shorter.
>
> Yes, that makes sense to me.
> And so I think the patch should also increase 251 to 252.

Thanks for pointing that out. I appreciate the feedback
and will send an updated version with the suggested change.
Regards

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

end of thread, other threads:[~2025-04-16 15:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-12 16:06 [PATCH net-next] net: ipconfig: replace strncpy with strscpy Pranav Tyagi
2025-04-15 16:35 ` Simon Horman
2025-04-16  0:19   ` Jakub Kicinski
2025-04-16 11:17     ` Simon Horman
2025-04-16 15:01       ` Pranav Tyagi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).