public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 1/2] packet: Avoid shadowing index(3)
Date: Tue, 19 Sep 2023 11:05:27 +1000	[thread overview]
Message-ID: <ZQjz10U9LI9noHxh@zatzit> (raw)
In-Reply-To: <20230918101524.3cc2fee7@elisabeth>

[-- Attachment #1: Type: text/plain, Size: 2571 bytes --]

On Mon, Sep 18, 2023 at 10:16:00AM +0200, Stefano Brivio wrote:
> On Fri, 15 Sep 2023 16:43:36 +1000
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > A classic gotcha of the standard C library is that its unwise to call any
> > variable 'index' because it will shadow the standard string library
> > function index(3).  This can cause warnings from cppcheck amongst others,
> > and it also means that if the variable is removed you tend to get confusing
> > type errors (or sometimes nothing at all) instead of a nice simple "name is
> > not defined" error.
> > 
> > Strictly speaking this only occurs if <string.h> is included, but that is
> > so common that as a rule it's best to just avoid it always.  We have a few
> > places in packet.[ch] which hit this trap so rename the variables to avoid
> > it.
> > 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  packet.c | 28 ++++++++++++++--------------
> >  packet.h | 10 +++++-----
> >  2 files changed, 19 insertions(+), 19 deletions(-)
> > 
> > diff --git a/packet.c b/packet.c
> > index ce807e2..8e3a87c 100644
> > --- a/packet.c
> > +++ b/packet.c
> > @@ -33,11 +33,11 @@
> >  void packet_add_do(struct pool *p, size_t len, const char *start,
> >  		   const char *func, int line)
> >  {
> > -	size_t index = p->count;
> > +	size_t idx = p->count;
> >  
> > -	if (index >= p->size) {
> > +	if (idx >= p->size) {
> >  		trace("add packet index %lu to pool with size %lu, %s:%i",
> > -		      index, p->size, func, line);
> > +		      idx, p->size, func, line);
> >  		return;
> >  	}
> >  
> > @@ -66,8 +66,8 @@ void packet_add_do(struct pool *p, size_t len, const char *start,
> >  	}
> >  #endif
> >  
> > -	p->pkt[index].offset = start - p->buf;
> > -	p->pkt[index].len = len;
> > +	p->pkt[idx].offset = start - p->buf;
> > +	p->pkt[idx].len = len;
> >  
> >  	p->count++;
> >  }
> > @@ -84,13 +84,13 @@ void packet_add_do(struct pool *p, size_t len, const char *start,
> >   *
> >   * Return: pointer to start of data range, NULL on invalid range or descriptor
> >   */
> > -void *packet_get_do(const struct pool *p, size_t index, size_t offset,
> > +void *packet_get_do(const struct pool *p, size_t idx, size_t offset,
> 
> Really minor, but... the comment about @index should be updated as well.

Oops, yes.  Fixed.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-09-19  2:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-15  6:43 [PATCH 0/2] Some static checker fixes David Gibson
2023-09-15  6:43 ` [PATCH 1/2] packet: Avoid shadowing index(3) David Gibson
2023-09-18  8:16   ` Stefano Brivio
2023-09-19  1:05     ` David Gibson [this message]
2023-09-15  6:43 ` [PATCH 2/2] util: Consolidate and improve workarounds for clang-tidy issue 58992 David Gibson
2023-09-18  8:16   ` Stefano Brivio
2023-09-19  1:08     ` David Gibson
2023-09-19 22:17       ` Stefano Brivio

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=ZQjz10U9LI9noHxh@zatzit \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).