From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id D6F655A0271 for ; Tue, 19 Sep 2023 04:09:13 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695089349; bh=/duG61VEi89rs1BSdQ6YcytKbbnYyzHZb//s5QjoqNM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=e0/39OB42NUOvtmPGOmzg/T2UCOQD7SKPmEa7TtChKSxsAC8r6RirT3IbLLmZnQ9U Xi05w7kp3VwOJWdW+vkRyBW/b9tlP78synnuAEIgF8lV9mRDl3sQn7W3jH01GMjalo qin56GZhyDOyyeZKH/vqTy3OuntSS5nIbOf11v+s= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RqQ8571ZGz4xM6; Tue, 19 Sep 2023 12:09:09 +1000 (AEST) Date: Tue, 19 Sep 2023 11:05:27 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 1/2] packet: Avoid shadowing index(3) Message-ID: References: <20230915064337.2380211-1-david@gibson.dropbear.id.au> <20230915064337.2380211-2-david@gibson.dropbear.id.au> <20230918101524.3cc2fee7@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Bcd0LOKfu1mrNEp3" Content-Disposition: inline In-Reply-To: <20230918101524.3cc2fee7@elisabeth> Message-ID-Hash: VLXVL5SCZZGVGC6YL5VT33EUDYAGYM2O X-Message-ID-Hash: VLXVL5SCZZGVGC6YL5VT33EUDYAGYM2O X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --Bcd0LOKfu1mrNEp3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Sep 18, 2023 at 10:16:00AM +0200, Stefano Brivio wrote: > On Fri, 15 Sep 2023 16:43:36 +1000 > David Gibson wrote: >=20 > > A classic gotcha of the standard C library is that its unwise to call a= ny > > variable 'index' because it will shadow the standard string library > > function index(3). This can cause warnings from cppcheck amongst other= s, > > and it also means that if the variable is removed you tend to get confu= sing > > type errors (or sometimes nothing at all) instead of a nice simple "nam= e is > > not defined" error. > >=20 > > Strictly speaking this only occurs if 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 av= oid > > it. > >=20 > > Signed-off-by: David Gibson > > --- > > packet.c | 28 ++++++++++++++-------------- > > packet.h | 10 +++++----- > > 2 files changed, 19 insertions(+), 19 deletions(-) > >=20 > > 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 =3D p->count; > > + size_t idx =3D p->count; > > =20 > > - if (index >=3D p->size) { > > + if (idx >=3D 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; > > } > > =20 > > @@ -66,8 +66,8 @@ void packet_add_do(struct pool *p, size_t len, const = char *start, > > } > > #endif > > =20 > > - p->pkt[index].offset =3D start - p->buf; > > - p->pkt[index].len =3D len; > > + p->pkt[idx].offset =3D start - p->buf; > > + p->pkt[idx].len =3D len; > > =20 > > p->count++; > > } > > @@ -84,13 +84,13 @@ void packet_add_do(struct pool *p, size_t len, cons= t char *start, > > * > > * Return: pointer to start of data range, NULL on invalid range or de= scriptor > > */ > > -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, >=20 > Really minor, but... the comment about @index should be updated as well. Oops, yes. Fixed. --=20 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 --Bcd0LOKfu1mrNEp3 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmUI89AACgkQzQJF27ox 2GccPQ/9Eco5U9FOrMscUsM7vLnzuK1LwrkAkTint1JvW+ysuhmvLQt2kTLKGJrr /Bg1Viu29426pOofib3xirQMLCDts9qj3oWQ41XrJyCygz6iMEymGEfK0KRQWSe9 2XlsLzsHKDYMNXg5Wa+fnSMPZBZxYpliYkM4xJ7tPdiJ2JMzf4/Pav7Yy0LGsJfs 5YAwPTNCr+jVrogtxiC1OpjDuIpVuk7XWsmFcLywR3DLQBbWCiOwZlxbFTcryQKK IZBwCoAeBKzLuFTMTIRpuw9IN0301Z4wayEA68hlGBQASM36GCRrDtdSyQYnYVao maHBuCel3k2rdVuzmyRxEMBy1CHygz0VEd9cGm6nfvTEvQYGzb8qjmMy8Ppi1hov chTZjz6tpXSmeucnSolVU0v/fIGefHwAVxy1hWjTWwrCKPWWlfiY4vfoeoqpnjU5 UPEKmuevoBz0rjojyVgvDgIdPkYhwNvaFWcpoboUMiHSsTzfsAQ7vrUo6Y0wqeGE 7MaQSLfFZo17brtOxKFe3Yk5I5L9Ai6cGxy+fOe5ueS20dgbyZ+4wnd3AN8P7CsK wZ06dnPYbMkx7r4lyrjSmBr10nNXm07gMskSc8Hf3FA361k3CVFNZkQ91GGDfF3N jCHXiOHUfW/hmEMZ7kpZzNo/gZGRzgpdXGcnDfbcTFGrROrk8Dw= =azg9 -----END PGP SIGNATURE----- --Bcd0LOKfu1mrNEp3--