netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Jakub Kicinski' <kuba@kernel.org>, Andrew Lunn <andrew@lunn.ch>
Cc: netdev <netdev@vger.kernel.org>
Subject: RE: [PATCH net-next] drivers: net: sky2: Fix -Wstringop-truncation with W=1
Date: Tue, 3 Nov 2020 10:19:55 +0000	[thread overview]
Message-ID: <c3c5682a5953429987bb5d30d631daa7@AcuMS.aculab.com> (raw)
In-Reply-To: <20201102160106.29edcc11@kicinski-fedora-PC1C0HJN.hsd1.ca.comcast.net>

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)

  reply	other threads:[~2020-11-03 10:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2020-11-03 16:25     ` Stephen Hemminger
2020-11-03 16:26       ` Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c3c5682a5953429987bb5d30d631daa7@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=andrew@lunn.ch \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).