From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202410 header.b=ZFBCBg9z; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id DFAEF5A061A for ; Fri, 08 Nov 2024 03:23:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1731032623; bh=6al4xMhpQbCtnj1yIlVrqyCqOW4ea69ymFHwAnzvQek=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZFBCBg9zzfW7IoOHREcsWx8i+2f/u4P9nR9GvkRBncCfcbKJrbDHmzshrvx+1d4W3 h6z1js7Jjw/+V/FNQi1QG6cVY+motV2cTCsto5b4UuMsf2rcnpykgjW/GINK1EEeMl d6Y8t6xbc256XxhBGefW2oLCn8UcvubDyJVcCjyKGqGTiZzLgZ0bXozAGpLD6GpEcd Z5osScd1McwBb4fRDsBso2yfXQ2t1AnD8OsPwIxLcJs8XxMU+cyRGCxRfL1U0gnakI WUhfV17g0Ktn2ghMeIXaBJQbh+kgxwIxDVYIagILytZqpx70d6Ar4icjp14LCIN3tn XJW22kb9tOO2w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Xl2mv20Y9z4wbr; Fri, 8 Nov 2024 13:23:43 +1100 (AEDT) Date: Fri, 8 Nov 2024 11:26:12 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 1/6] dhcpv6: Use for loop instead of goto to avoid false positive cppcheck warning Message-ID: References: <20241107184331.3164784-1-sbrivio@redhat.com> <20241107184331.3164784-2-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="Vn6V2bYY9oEQkk+u" Content-Disposition: inline In-Reply-To: <20241107184331.3164784-2-sbrivio@redhat.com> Message-ID-Hash: 3DIV7XKDHOSR3DRNZGGK772OZHUWGQBH X-Message-ID-Hash: 3DIV7XKDHOSR3DRNZGGK772OZHUWGQBH 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: --Vn6V2bYY9oEQkk+u Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Nov 07, 2024 at 07:43:26PM +0100, Stefano Brivio wrote: > cppcheck 2.16.0 reports: >=20 > dhcpv6.c:334:14: style: The comparison 'ia_type =3D=3D 3' is always true.= [knownConditionTrueFalse] > if (ia_type =3D=3D OPT_IA_NA) { > ^ > dhcpv6.c:306:12: note: 'ia_type' is assigned value '3' here. > ia_type =3D OPT_IA_NA; > ^ > dhcpv6.c:334:14: note: The comparison 'ia_type =3D=3D 3' is always true. > if (ia_type =3D=3D OPT_IA_NA) { > ^ >=20 > this is not really the case as we set ia_type to OPT_IA_TA and then > jump back. >=20 > Anyway, there's no particular reason to use a goto here: add a trivial > foreach() macro to go through elements of an array and use it instead. >=20 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > dhcpv6.c | 47 +++++++++++++++++++++-------------------------- > util.h | 3 +++ > 2 files changed, 24 insertions(+), 26 deletions(-) >=20 > diff --git a/dhcpv6.c b/dhcpv6.c > index 14a5c7e..f2e7307 100644 > --- a/dhcpv6.c > +++ b/dhcpv6.c > @@ -296,47 +296,42 @@ static struct opt_hdr *dhcpv6_opt(const struct pool= *p, size_t *offset, > static struct opt_hdr *dhcpv6_ia_notonlink(const struct pool *p, > struct in6_addr *la) > { > + int ia_types[2] =3D { OPT_IA_NA, OPT_IA_TA }, *ia_type; > + const struct opt_ia_addr *opt_addr; > char buf[INET6_ADDRSTRLEN]; > struct in6_addr req_addr; > const struct opt_hdr *h; > struct opt_hdr *ia; > size_t offset; > - int ia_type; > =20 > - ia_type =3D OPT_IA_NA; > -ia_ta: > - offset =3D 0; > - while ((ia =3D dhcpv6_opt(p, &offset, ia_type))) { > - if (ntohs(ia->l) < OPT_VSIZE(ia_na)) > - return NULL; > + foreach(ia_type, ia_types) { > + offset =3D 0; > + while ((ia =3D dhcpv6_opt(p, &offset, *ia_type))) { > + if (ntohs(ia->l) < OPT_VSIZE(ia_na)) > + return NULL; > =20 > - offset +=3D sizeof(struct opt_ia_na); > + offset +=3D sizeof(struct opt_ia_na); > =20 > - while ((h =3D dhcpv6_opt(p, &offset, OPT_IAAADR))) { > - const struct opt_ia_addr *opt_addr; > + while ((h =3D dhcpv6_opt(p, &offset, OPT_IAAADR))) { > + if (ntohs(h->l) !=3D OPT_VSIZE(ia_addr)) > + return NULL; > =20 > - if (ntohs(h->l) !=3D OPT_VSIZE(ia_addr)) > - return NULL; > + opt_addr =3D (const struct opt_ia_addr *)h; > + req_addr =3D opt_addr->addr; > + if (!IN6_ARE_ADDR_EQUAL(la, &req_addr)) > + goto err; > =20 > - opt_addr =3D (const struct opt_ia_addr *)h; > - req_addr =3D opt_addr->addr; > - if (!IN6_ARE_ADDR_EQUAL(la, &req_addr)) { > - info("DHCPv6: requested address %s not on link", > - inet_ntop(AF_INET6, &req_addr, > - buf, sizeof(buf))); > - return ia; > + offset +=3D sizeof(struct opt_ia_addr); > } > - > - offset +=3D sizeof(struct opt_ia_addr); > } > } > =20 > - if (ia_type =3D=3D OPT_IA_NA) { > - ia_type =3D OPT_IA_TA; > - goto ia_ta; > - } > - > return NULL; > + > +err: > + info("DHCPv6: requested address %s not on link", > + inet_ntop(AF_INET6, &req_addr, buf, sizeof(buf))); > + return ia; > } > =20 > /** > diff --git a/util.h b/util.h > index 0bf396a..582ef57 100644 > --- a/util.h > +++ b/util.h > @@ -102,6 +102,9 @@ > =20 > #define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0]))) > =20 > +#define foreach(item, array) \ > + for ((item) =3D (array); (item) - (array) < ARRAY_SIZE(array); (item)++) > + > #define IN_INTERVAL(a, b, x) ((x) >=3D (a) && (x) <=3D (b)) > #define FD_PROTO(x, proto) \ > (IN_INTERVAL(c->proto.fd_min, c->proto.fd_max, (x))) --=20 David Gibson (he or they) | 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 --Vn6V2bYY9oEQkk+u Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmctWqMACgkQzQJF27ox 2Gc5ExAAoLfK2kBIOOW4+pT3UOH3HfWXffSKbR9GZzq2huOlIDXfnDKmytbfr9ip iK0xRVHZAbKpog19B3MqXr2Y1HrzLhwV0dQFgYwjlle97JeN2243dhobiAFf5l/e gbiGXEogV6pDvEWSXsar8hhrm74RHhCiC3gRmfx2Ody/lgrFB3if1COuEgKtVOGF 8Jnq0Jchk/GOmID1UaFHehCaqLpBqbl6Ffr/TzX3y21xWcuY1mE5ptcbh2WdJAcn v5OFKpyxTP1ATUqTzUpZlBYiVwp3sbtzcZfZfiLn9DOAZmYtmM/gNknYog0tThe6 v/sW3OkcvRE5WHlQVfppEW2zKf72jLJtLove5VY4FWly5H20j/O4FzmmY5zoJNM2 nkNwp6MjjEzec/YAqDTqNQLQ6vQzgMSSa95jePYO0xeWP5tta2vDdd5ZkbYuEFRQ vnOYe5Hw1S0p+Hq4Qi3QK+SUDbqtEc+tT1GuWH5qMML1OfEAAE8VhISa8+DkT2/w W38/q04MrZ+VkHrgM4dWxHTHflvvHxspOjaWLXyBjz4o8NDAbgCaa+4drV1sBlR3 ibgQdvhN50fE3I1/RHmRj1KJ0wEVjFNbi95alQ9cTv0dwPTKwuVuuJc73oaJzq13 1dM6Jm6kPL3onwRCqUQWIkOS+tT0wcBPW1MbKsa+1AZkvtK0Koo= =SFdo -----END PGP SIGNATURE----- --Vn6V2bYY9oEQkk+u--