public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH v2] cppcheck: Work around some cppcheck 2.15.0 redundantInitialization warnings
@ 2024-09-05 11:22 David Gibson
  2024-09-06 11:29 ` Stefano Brivio
  0 siblings, 1 reply; 2+ messages in thread
From: David Gibson @ 2024-09-05 11:22 UTC (permalink / raw)
  To: Stefano Brivio, passt-dev; +Cc: David Gibson

cppcheck-2.15.0 has apparently broadened when it throws a warning about
redundant initialization to include some cases where we have an initializer
for some fields, but then set other fields in the function body.

This is arguably a false positive: although we are technically overwriting
the zero-initialization the compiler supplies for fields not explicitly
initialized, this sort of construct makes sense when there are some fields
we know at the top of the function where the initializer is, but others
that require more complex calculation.

That said, in the two places this shows up, it's pretty easy to work
around.  The results are arguably slightly clearer than what we had, since
they move the parts of the initialization closer together.

So do that rather than having ugly suppressions or dealing with the
tedious process of reporting a cppcheck false positive.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 pasta.c |  3 ++-
 udp.c   | 10 ++++------
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/pasta.c b/pasta.c
index 1900693d..307fb4a7 100644
--- a/pasta.c
+++ b/pasta.c
@@ -427,12 +427,12 @@ static int pasta_netns_quit_timer(void)
  */
 void pasta_netns_quit_init(const struct ctx *c)
 {
-	union epoll_ref ref = { .type = EPOLL_TYPE_NSQUIT_INOTIFY };
 	struct epoll_event ev = { .events = EPOLLIN };
 	int flags = O_NONBLOCK | O_CLOEXEC;
 	struct statfs s = { 0 };
 	bool try_inotify = true;
 	int fd = -1, dir_fd;
+	union epoll_ref ref;
 
 	if (c->mode != MODE_PASTA || c->no_netns_quit || !*c->netns_base)
 		return;
@@ -463,6 +463,7 @@ void pasta_netns_quit_init(const struct ctx *c)
 		ref.type = EPOLL_TYPE_NSQUIT_TIMER;
 	} else {
 		close(dir_fd);
+		ref.type = EPOLL_TYPE_NSQUIT_INOTIFY;
 	}
 
 	if (fd > FD_REF_MAX)
diff --git a/udp.c b/udp.c
index bd9051e1..aae6d142 100644
--- a/udp.c
+++ b/udp.c
@@ -713,16 +713,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 		  const void *addr, const char *ifname, in_port_t port)
 {
-	union udp_listen_epoll_ref uref = { .port = port };
+	union udp_listen_epoll_ref uref = {
+		.pif = ns ? PIF_SPLICE : PIF_HOST,
+		.port = port,
+	};
 	int r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
 
 	ASSERT(!c->no_udp);
 
-	if (ns)
-		uref.pif = PIF_SPLICE;
-	else
-		uref.pif = PIF_HOST;
-
 	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
 		int s;
 
-- 
@@ -713,16 +713,14 @@ int udp_tap_handler(const struct ctx *c, uint8_t pif,
 int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
 		  const void *addr, const char *ifname, in_port_t port)
 {
-	union udp_listen_epoll_ref uref = { .port = port };
+	union udp_listen_epoll_ref uref = {
+		.pif = ns ? PIF_SPLICE : PIF_HOST,
+		.port = port,
+	};
 	int r4 = FD_REF_MAX + 1, r6 = FD_REF_MAX + 1;
 
 	ASSERT(!c->no_udp);
 
-	if (ns)
-		uref.pif = PIF_SPLICE;
-	else
-		uref.pif = PIF_HOST;
-
 	if (af == AF_UNSPEC && c->ifi4 && c->ifi6) {
 		int s;
 
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] cppcheck: Work around some cppcheck 2.15.0 redundantInitialization warnings
  2024-09-05 11:22 [PATCH v2] cppcheck: Work around some cppcheck 2.15.0 redundantInitialization warnings David Gibson
@ 2024-09-06 11:29 ` Stefano Brivio
  0 siblings, 0 replies; 2+ messages in thread
From: Stefano Brivio @ 2024-09-06 11:29 UTC (permalink / raw)
  To: David Gibson; +Cc: passt-dev

On Thu,  5 Sep 2024 21:22:04 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> cppcheck-2.15.0 has apparently broadened when it throws a warning about
> redundant initialization to include some cases where we have an initializer
> for some fields, but then set other fields in the function body.
> 
> This is arguably a false positive: although we are technically overwriting
> the zero-initialization the compiler supplies for fields not explicitly
> initialized, this sort of construct makes sense when there are some fields
> we know at the top of the function where the initializer is, but others
> that require more complex calculation.
> 
> That said, in the two places this shows up, it's pretty easy to work
> around.  The results are arguably slightly clearer than what we had, since
> they move the parts of the initialization closer together.
> 
> So do that rather than having ugly suppressions or dealing with the
> tedious process of reporting a cppcheck false positive.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

Applied.

-- 
Stefano


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-09-06 11:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-05 11:22 [PATCH v2] cppcheck: Work around some cppcheck 2.15.0 redundantInitialization warnings David Gibson
2024-09-06 11:29 ` Stefano Brivio

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).