All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
@ 2020-10-31 17:40 Andrew Lunn
  2020-11-03  0:01 ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Lunn @ 2020-10-31 17:40 UTC (permalink / raw)
  To: Jakub Kicinski; +Cc: netdev, Andrew Lunn

In function ‘strncpy’,
    inlined from ‘sky2_name’ at drivers/net/ethernet/marvell/sky2.c:4903:3,
    inlined from ‘sky2_probe’ at drivers/net/ethernet/marvell/sky2.c:5049:2:
./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]

None of the device names are 16 characters long, so it was never an
issue, but reduce the length of the buffer size by one to avoid the
warning.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
---
 drivers/net/ethernet/marvell/sky2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
index 25981a7a43b5..35b0ec5afe13 100644
--- a/drivers/net/ethernet/marvell/sky2.c
+++ b/drivers/net/ethernet/marvell/sky2.c
@@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
 	};
 
 	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
-		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
+		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz - 1);
 	else
 		snprintf(buf, sz, "(chip %#x)", chipid);
 	return buf;
-- 
2.28.0


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

* Re: [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
  2020-10-31 17:40 [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1 Andrew Lunn
@ 2020-11-03  0:01 ` Jakub Kicinski
  2020-11-03 10:19   ` David Laight
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2020-11-03  0:01 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: netdev

On Sat, 31 Oct 2020 18:40:28 +0100 Andrew Lunn wrote:
> In function ‘strncpy’,
>     inlined from ‘sky2_name’ at drivers/net/ethernet/marvell/sky2.c:4903:3,
>     inlined from ‘sky2_probe’ at drivers/net/ethernet/marvell/sky2.c:5049:2:
> ./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Wstringop-truncation]
> 
> None of the device names are 16 characters long, so it was never an
> issue, but reduce the length of the buffer size by one to avoid the
> warning.
> 
> Signed-off-by: Andrew Lunn <andrew@lunn.ch>
> ---
>  drivers/net/ethernet/marvell/sky2.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
> index 25981a7a43b5..35b0ec5afe13 100644
> --- a/drivers/net/ethernet/marvell/sky2.c
> +++ b/drivers/net/ethernet/marvell/sky2.c
> @@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
>  	};
>  
>  	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
> -		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
> +		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz - 1);

Hm. This irks the eye a little. AFAIK the idiomatic code would be:

	strncpy(buf, name..., sz - 1);
	buf[sz - 1] = '\0';

Perhaps it's easier to convert to strscpy()/strscpy_pad()?

>  	else
>  		snprintf(buf, sz, "(chip %#x)", chipid);
>  	return buf;


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

* RE: [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
  2020-11-03  0:01 ` Jakub Kicinski
@ 2020-11-03 10:19   ` David Laight
  2020-11-03 16:25     ` Stephen Hemminger
  0 siblings, 1 reply; 5+ messages in thread
From: David Laight @ 2020-11-03 10:19 UTC (permalink / raw)
  To: 'Jakub Kicinski', Andrew Lunn; +Cc: netdev

From: Jakub Kicinski
> Sent: 03 November 2020 00:01
> 
> On Sat, 31 Oct 2020 18:40:28 +0100 Andrew Lunn wrote:
> > In function ‘strncpy’,
> >     inlined from ‘sky2_name’ at drivers/net/ethernet/marvell/sky2.c:4903:3,
> >     inlined from ‘sky2_probe’ at drivers/net/ethernet/marvell/sky2.c:5049:2:
> > ./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 16 equals destination
> size [-Wstringop-truncation]
> >
> > None of the device names are 16 characters long, so it was never an
> > issue, but reduce the length of the buffer size by one to avoid the
> > warning.
> >
> > Signed-off-by: Andrew Lunn <andrew@lunn.ch>
> > ---
> >  drivers/net/ethernet/marvell/sky2.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
> > index 25981a7a43b5..35b0ec5afe13 100644
> > --- a/drivers/net/ethernet/marvell/sky2.c
> > +++ b/drivers/net/ethernet/marvell/sky2.c
> > @@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
> >  	};
> >
> >  	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
> > -		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
> > +		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz - 1);
> 
> Hm. This irks the eye a little. AFAIK the idiomatic code would be:
> 
> 	strncpy(buf, name..., sz - 1);
> 	buf[sz - 1] = '\0';
> 
> Perhaps it's easier to convert to strscpy()/strscpy_pad()?
> 
> >  	else
> >  		snprintf(buf, sz, "(chip %#x)", chipid);
> >  	return buf;

Is the pad needed?
It isn't present in the 'else' branch.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

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

* Re: [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
  2020-11-03 10:19   ` David Laight
@ 2020-11-03 16:25     ` Stephen Hemminger
  2020-11-03 16:26       ` Stephen Hemminger
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Hemminger @ 2020-11-03 16:25 UTC (permalink / raw)
  To: David Laight; +Cc: 'Jakub Kicinski', Andrew Lunn, netdev

On Tue, 3 Nov 2020 10:19:55 +0000
David Laight <David.Laight@ACULAB.COM> wrote:

> From: Jakub Kicinski
> > Sent: 03 November 2020 00:01
> > 
> > On Sat, 31 Oct 2020 18:40:28 +0100 Andrew Lunn wrote:  
> > > In function ‘strncpy’,
> > >     inlined from ‘sky2_name’ at drivers/net/ethernet/marvell/sky2.c:4903:3,
> > >     inlined from ‘sky2_probe’ at drivers/net/ethernet/marvell/sky2.c:5049:2:
> > > ./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 16 equals destination  
> > size [-Wstringop-truncation]  
> > >
> > > None of the device names are 16 characters long, so it was never an
> > > issue, but reduce the length of the buffer size by one to avoid the
> > > warning.
> > >
> > > Signed-off-by: Andrew Lunn <andrew@lunn.ch>
> > > ---
> > >  drivers/net/ethernet/marvell/sky2.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
> > > index 25981a7a43b5..35b0ec5afe13 100644
> > > --- a/drivers/net/ethernet/marvell/sky2.c
> > > +++ b/drivers/net/ethernet/marvell/sky2.c
> > > @@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
> > >  	};
> > >
> > >  	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
> > > -		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
> > > +		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz - 1);  
> > 
> > Hm. This irks the eye a little. AFAIK the idiomatic code would be:
> > 
> > 	strncpy(buf, name..., sz - 1);
> > 	buf[sz - 1] = '\0';
> > 
> > Perhaps it's easier to convert to strscpy()/strscpy_pad()?
> >   
> > >  	else
> > >  		snprintf(buf, sz, "(chip %#x)", chipid);
> > >  	return buf;  
> 
> Is the pad needed?
> It isn't present in the 'else' branch.

Since this is non-critical code and is only ther to print something useful
on boot, why not just use snprintf on both sides of statement?

diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
index 25981a7a43b5..96edad30006e 100644
--- a/drivers/net/ethernet/marvell/sky2.c
+++ b/drivers/net/ethernet/marvell/sky2.c
@@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
        };
 
        if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
-               strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
+               snprintf(buf, sz, name[chipid - CHIP_ID_YUKON_XL]);
        else
                snprintf(buf, sz, "(chip %#x)", chipid);
        return buf

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

* Re: [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
  2020-11-03 16:25     ` Stephen Hemminger
@ 2020-11-03 16:26       ` Stephen Hemminger
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Hemminger @ 2020-11-03 16:26 UTC (permalink / raw)
  To: David Laight; +Cc: 'Jakub Kicinski', Andrew Lunn, netdev

On Tue, 3 Nov 2020 08:25:01 -0800
Stephen Hemminger <stephen@networkplumber.org> wrote:

> On Tue, 3 Nov 2020 10:19:55 +0000
> David Laight <David.Laight@ACULAB.COM> wrote:
> 
> > From: Jakub Kicinski  
> > > Sent: 03 November 2020 00:01
> > > 
> > > On Sat, 31 Oct 2020 18:40:28 +0100 Andrew Lunn wrote:    
> > > > In function ‘strncpy’,
> > > >     inlined from ‘sky2_name’ at drivers/net/ethernet/marvell/sky2.c:4903:3,
> > > >     inlined from ‘sky2_probe’ at drivers/net/ethernet/marvell/sky2.c:5049:2:
> > > > ./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 16 equals destination    
> > > size [-Wstringop-truncation]    
> > > >
> > > > None of the device names are 16 characters long, so it was never an
> > > > issue, but reduce the length of the buffer size by one to avoid the
> > > > warning.
> > > >
> > > > Signed-off-by: Andrew Lunn <andrew@lunn.ch>
> > > > ---
> > > >  drivers/net/ethernet/marvell/sky2.c | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
> > > > index 25981a7a43b5..35b0ec5afe13 100644
> > > > --- a/drivers/net/ethernet/marvell/sky2.c
> > > > +++ b/drivers/net/ethernet/marvell/sky2.c
> > > > @@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
> > > >  	};
> > > >
> > > >  	if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
> > > > -		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
> > > > +		strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz - 1);    
> > > 
> > > Hm. This irks the eye a little. AFAIK the idiomatic code would be:
> > > 
> > > 	strncpy(buf, name..., sz - 1);
> > > 	buf[sz - 1] = '\0';
> > > 
> > > Perhaps it's easier to convert to strscpy()/strscpy_pad()?
> > >     
> > > >  	else
> > > >  		snprintf(buf, sz, "(chip %#x)", chipid);
> > > >  	return buf;    
> > 
> > Is the pad needed?
> > It isn't present in the 'else' branch.  
> 
> Since this is non-critical code and is only ther to print something useful
> on boot, why not just use snprintf on both sides of statement?

Like this is what I meant...
diff --git a/drivers/net/ethernet/marvell/sky2.c b/drivers/net/ethernet/marvell/sky2.c
index 25981a7a43b5..ebe1406c6e64 100644
--- a/drivers/net/ethernet/marvell/sky2.c
+++ b/drivers/net/ethernet/marvell/sky2.c
@@ -4900,7 +4900,7 @@ static const char *sky2_name(u8 chipid, char *buf, int sz)
        };
 
        if (chipid >= CHIP_ID_YUKON_XL && chipid <= CHIP_ID_YUKON_OP_2)
-               strncpy(buf, name[chipid - CHIP_ID_YUKON_XL], sz);
+               snprintf(buf, sz, "%s", name[chipid - CHIP_ID_YUKON_XL]);
        else
                snprintf(buf, sz, "(chip %#x)", chipid);
        return buf;



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

end of thread, other threads:[~2020-11-03 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-31 17:40 [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1 Andrew Lunn
2020-11-03  0:01 ` Jakub Kicinski
2020-11-03 10:19   ` David Laight
2020-11-03 16:25     ` Stephen Hemminger
2020-11-03 16:26       ` Stephen Hemminger

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.