All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kurt Van Dijck <kurt.van.dijck@eia.be>
To: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: Alexander Stein <alexander.stein@systec-electronic.com>,
	linux-can@vger.kernel.org
Subject: Re: [PATCH] Use strcmp instead of strncmp
Date: Wed, 14 Nov 2012 09:55:37 +0100	[thread overview]
Message-ID: <20121114085537.GB82180@airbook.eia.lan> (raw)
In-Reply-To: <50A2AD0A.6040005@pengutronix.de>

On Tue, Nov 13, 2012 at 09:26:50PM +0100, Marc Kleine-Budde wrote:
> On 11/13/2012 04:47 PM, Alexander Stein wrote:
> > This bug was detected by the clang warning:
> > libsocketcan.c:384:16: warning: argument to 'sizeof' in 'strncmp' call
> > is the same expression as the source; did you mean to provide an
> > explicit length? [-Wsizeof-pointer-memaccess]
> > sizeof(name)) != 0)
> > ~~~~~~~^~~~~~
strlen() requires a null terminator. Is that always present?
Unfortunately, I've no access to the libsocketcan code today...
> > 
> > Signed-off-by: Alexander Stein <alexander.stein@systec-electronic.com>
> > ---
> >  src/libsocketcan.c |    3 ++-
> >  1 files changed, 2 insertions(+), 1 deletions(-)
> > 
> > diff --git a/src/libsocketcan.c b/src/libsocketcan.c
> > index fedcbdc..3ad2a6c 100644
> > --- a/src/libsocketcan.c
> > +++ b/src/libsocketcan.c
> > @@ -364,6 +364,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
> >  		     nl_msg = NLMSG_NEXT(nl_msg, u_msglen)) {
> >  			int type = nl_msg->nlmsg_type;
> >  			int len;
> > +			int namelen = strlen(name);
> >  
> >  			if (type == NLMSG_DONE) {
> >  				done++;
> > @@ -381,7 +382,7 @@ static int do_get_nl_link(int fd, __u8 acquire, const char *name, void *res)
> >  
> >  			if (strncmp
> >  			    ((char *)RTA_DATA(tb[IFLA_IFNAME]), name,
> > -			     sizeof(name)) != 0)
> > +			     namelen) != 0)
> 
> What about using strcmp() instead. AFAIK it will compare until it finds
> the end of the string in one of the strings, so it should be no
> difference here?
if name contains a null terminator, using strcmp() is even better.
> 
> >  				continue;
> >  
> >  			if (tb[IFLA_LINKINFO])
> > 
> 
> Marc
> 
> -- 
> Pengutronix e.K.                  | Marc Kleine-Budde           |
> Industrial Linux Solutions        | Phone: +49-231-2826-924     |
> Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
> Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
> 



  reply	other threads:[~2012-11-14  8:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-13 15:47 [PATCH] Use strlen instead of sizeof on char* string Alexander Stein
2012-11-13 20:26 ` Marc Kleine-Budde
2012-11-14  8:55   ` Kurt Van Dijck [this message]
2012-11-14  9:07     ` [PATCH] Use strcmp instead of strncmp Alexander Stein
2012-11-14 10:04       ` Kurt Van Dijck
2012-11-14 10:10         ` Marc Kleine-Budde

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=20121114085537.GB82180@airbook.eia.lan \
    --to=kurt.van.dijck@eia.be \
    --cc=alexander.stein@systec-electronic.com \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    /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 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.