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>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH] cppcheck,valgrind: Don't pass NULL to recv() with MSG_TRUNC
Date: Wed, 15 Nov 2023 15:41:24 +1100	[thread overview]
Message-ID: <20231115044124.1496698-1-david@gibson.dropbear.id.au> (raw)

Usually, of course, it's invalid to pass a NULL buffer to recv().  However,
it's acceptable when using MSG_TRUNC, because that suppresses actually
writing to the buffer.  So, we pass NULL in tcp_sock_consume().

Unfortunately, checker tools aren't always aware of that special case: we
already have a suppression for cppcheck to cover this.  valgrind-3.22.0
(present in Fedora 39) has a similar problem, generating a spurious warning
here.

We could generate another suppression for valgrind, however, it so happens
that we already have tcp_buf_discard ready to hand.  If we pass this
instead of NULL it makes both cppcheck and valgrind happy.  We're still
using the MSG_TRUNC flag, the kernel doesn't actually have to copy data,
so we should still have the performance benefits of it.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tcp.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/tcp.c b/tcp.c
index cfcd40a..f51d27a 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2106,8 +2106,13 @@ static int tcp_sock_consume(const struct tcp_tap_conn *conn, uint32_t ack_seq)
 	if (SEQ_LE(ack_seq, conn->seq_ack_from_tap))
 		return 0;
 
-	/* cppcheck-suppress [nullPointer, unmatchedSuppression] */
-	if (recv(conn->sock, NULL, ack_seq - conn->seq_ack_from_tap,
+	/* Since we're using MSG_TRUNC, it's allowed to pass NULL instead of a
+	 * real buffer.  However some checker tools (including at least some
+	 * versions of cppcheck and valgrind) aren't aware of that special case.
+	 * We so happen to have a convenient discard buffer, so we might as well
+	 * pass it to avoid spurious complaints from those tools.
+	 */
+	if (recv(conn->sock, tcp_buf_discard, ack_seq - conn->seq_ack_from_tap,
 		 MSG_DONTWAIT | MSG_TRUNC) < 0)
 		return -errno;
 
-- 
@@ -2106,8 +2106,13 @@ static int tcp_sock_consume(const struct tcp_tap_conn *conn, uint32_t ack_seq)
 	if (SEQ_LE(ack_seq, conn->seq_ack_from_tap))
 		return 0;
 
-	/* cppcheck-suppress [nullPointer, unmatchedSuppression] */
-	if (recv(conn->sock, NULL, ack_seq - conn->seq_ack_from_tap,
+	/* Since we're using MSG_TRUNC, it's allowed to pass NULL instead of a
+	 * real buffer.  However some checker tools (including at least some
+	 * versions of cppcheck and valgrind) aren't aware of that special case.
+	 * We so happen to have a convenient discard buffer, so we might as well
+	 * pass it to avoid spurious complaints from those tools.
+	 */
+	if (recv(conn->sock, tcp_buf_discard, ack_seq - conn->seq_ack_from_tap,
 		 MSG_DONTWAIT | MSG_TRUNC) < 0)
 		return -errno;
 
-- 
2.41.0


             reply	other threads:[~2023-11-15  4:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-15  4:41 David Gibson [this message]
2023-11-15  5:32 ` [PATCH] cppcheck,valgrind: Don't pass NULL to recv() with MSG_TRUNC Stefano Brivio
2023-11-15  8:11   ` David Gibson
2023-11-16  5:25     ` 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=20231115044124.1496698-1-david@gibson.dropbear.id.au \
    --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).