From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/6] dhcpv6: Use for loop instead of goto to avoid false positive cppcheck warning
Date: Thu, 7 Nov 2024 19:43:26 +0100 [thread overview]
Message-ID: <20241107184331.3164784-2-sbrivio@redhat.com> (raw)
In-Reply-To: <20241107184331.3164784-1-sbrivio@redhat.com>
cppcheck 2.16.0 reports:
dhcpv6.c:334:14: style: The comparison 'ia_type == 3' is always true. [knownConditionTrueFalse]
if (ia_type == OPT_IA_NA) {
^
dhcpv6.c:306:12: note: 'ia_type' is assigned value '3' here.
ia_type = OPT_IA_NA;
^
dhcpv6.c:334:14: note: The comparison 'ia_type == 3' is always true.
if (ia_type == OPT_IA_NA) {
^
this is not really the case as we set ia_type to OPT_IA_TA and then
jump back.
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.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
dhcpv6.c | 47 +++++++++++++++++++++--------------------------
util.h | 3 +++
2 files changed, 24 insertions(+), 26 deletions(-)
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] = { 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;
- ia_type = OPT_IA_NA;
-ia_ta:
- offset = 0;
- while ((ia = dhcpv6_opt(p, &offset, ia_type))) {
- if (ntohs(ia->l) < OPT_VSIZE(ia_na))
- return NULL;
+ foreach(ia_type, ia_types) {
+ offset = 0;
+ while ((ia = dhcpv6_opt(p, &offset, *ia_type))) {
+ if (ntohs(ia->l) < OPT_VSIZE(ia_na))
+ return NULL;
- offset += sizeof(struct opt_ia_na);
+ offset += sizeof(struct opt_ia_na);
- while ((h = dhcpv6_opt(p, &offset, OPT_IAAADR))) {
- const struct opt_ia_addr *opt_addr;
+ while ((h = dhcpv6_opt(p, &offset, OPT_IAAADR))) {
+ if (ntohs(h->l) != OPT_VSIZE(ia_addr))
+ return NULL;
- if (ntohs(h->l) != OPT_VSIZE(ia_addr))
- return NULL;
+ opt_addr = (const struct opt_ia_addr *)h;
+ req_addr = opt_addr->addr;
+ if (!IN6_ARE_ADDR_EQUAL(la, &req_addr))
+ goto err;
- opt_addr = (const struct opt_ia_addr *)h;
- req_addr = 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 += sizeof(struct opt_ia_addr);
}
-
- offset += sizeof(struct opt_ia_addr);
}
}
- if (ia_type == OPT_IA_NA) {
- ia_type = 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;
}
/**
diff --git a/util.h b/util.h
index 0bf396a..582ef57 100644
--- a/util.h
+++ b/util.h
@@ -102,6 +102,9 @@
#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
+#define foreach(item, array) \
+ for ((item) = (array); (item) - (array) < ARRAY_SIZE(array); (item)++)
+
#define IN_INTERVAL(a, b, x) ((x) >= (a) && (x) <= (b))
#define FD_PROTO(x, proto) \
(IN_INTERVAL(c->proto.fd_min, c->proto.fd_max, (x)))
--
@@ -102,6 +102,9 @@
#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
+#define foreach(item, array) \
+ for ((item) = (array); (item) - (array) < ARRAY_SIZE(array); (item)++)
+
#define IN_INTERVAL(a, b, x) ((x) >= (a) && (x) <= (b))
#define FD_PROTO(x, proto) \
(IN_INTERVAL(c->proto.fd_min, c->proto.fd_max, (x)))
--
2.43.0
next prev parent reply other threads:[~2024-11-07 18:43 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-07 18:43 [PATCH 0/6] Fix some more static checker warnings Stefano Brivio
2024-11-07 18:43 ` Stefano Brivio [this message]
2024-11-08 0:26 ` [PATCH 1/6] dhcpv6: Use for loop instead of goto to avoid false positive cppcheck warning David Gibson
2024-11-07 18:43 ` [PATCH 2/6] dhcpv6: Turn some option headers pointers to const Stefano Brivio
2024-11-08 0:27 ` David Gibson
2024-11-07 18:43 ` [PATCH 3/6] tap: Cast TAP_BUF_BYTES - ETH_MAX_MTU to ssize_t, not TAP_BUF_BYTES Stefano Brivio
2024-11-08 0:28 ` David Gibson
2024-11-07 18:43 ` [PATCH 4/6] util: Define small and big thresholds for socket buffers as unsigned long long Stefano Brivio
2024-11-08 0:29 ` David Gibson
2024-11-07 18:43 ` [PATCH 5/6] passt: Use NOLINT clang-tidy block instead of NOLINTNEXTLINE Stefano Brivio
2024-11-08 0:30 ` David Gibson
2024-11-07 18:43 ` [PATCH 6/6] tap, tcp, util: Add some missing SOCK_CLOEXEC flags Stefano Brivio
2024-11-08 0:31 ` David Gibson
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=20241107184331.3164784-2-sbrivio@redhat.com \
--to=sbrivio@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/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).