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=202602 header.b=HP1eFqQ6; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 10FD35A061D for ; Mon, 23 Mar 2026 09:33:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774254767; bh=dDRzaTEOVtQgnc4HEsLm2goStyMtr2zYyzC6ofWslxM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HP1eFqQ6xw8J6oWf6Xh9tSMzcqG5LsNtsB+orRbRPOzm+9cIQ05XUlHOysTCxS5oR +1Q4tj88GhCz3jFV9bhu5dqFza+spbUGmMvM5H4MFc+AmvLzRx4s7RiWHvH9bTl3Ze EzF9Qu5pwsDxEuRYus7CfsiKxrfpVFuAfiJZwV3oPYwx3SIEeiLp5Um059HOPhRUwB asADdHjdoGtrZIbjkOsp3S3+/AUT5RSV7hqsROkOKCG9lZYbkc+9C75e9a+e3tmU95 DXKjFGTU+31h45SuvfY6TaUuz+h95c8ODXtWbWdaxETWOsohrYw7tj3MA13pyAX/+5 biv9T/ng5fXdA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4ffRHz5Yy3z4wSK; Mon, 23 Mar 2026 19:32:47 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v3 19/25] ip: Define a bound for the string returned by ipproto_name() Date: Mon, 23 Mar 2026 18:37:26 +1100 Message-ID: <20260323073732.3158468-20-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260323073732.3158468-1-david@gibson.dropbear.id.au> References: <20260323073732.3158468-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ONGBEIERKSH6YOR6MRORNUVPYTR7QCS3 X-Message-ID-Hash: ONGBEIERKSH6YOR6MRORNUVPYTR7QCS3 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: David Gibson 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: ipproto_name() returns a static string of theoretically unbounded length. That's going to be inconvenient in future, so introduce IPPROTO_STRLEN giving an explicit bound on the length. Use static_assert() and some macros to ensure nothing we return can exceed this. Signed-off-by: David Gibson --- ip.c | 18 ++++++++++++------ ip.h | 1 + 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ip.c b/ip.c index 4e4e0bf5..f2506bb1 100644 --- a/ip.c +++ b/ip.c @@ -15,6 +15,7 @@ * Author: Stefano Brivio */ +#include #include #include @@ -24,7 +25,7 @@ * ipproto_name() - Get IP protocol name from number * @proto: IP protocol number * - * Return: pointer to name of protocol @proto + * Return: pointer to name of protocol @proto (<= IPPROTO_STRLEN bytes) * * Usually this would be done with getprotobynumber(3) but that reads * /etc/protocols and might allocate, which isn't possible for us once @@ -33,16 +34,21 @@ const char *ipproto_name(uint8_t proto) { switch (proto) { +#define CASE(s) \ + static_assert(sizeof(s) <= IPPROTO_STRLEN, \ + "Increase IPPROTO_STRLEN to contain " #s); \ + return s; case IPPROTO_ICMP: - return "ICMP"; + CASE("ICMP"); case IPPROTO_TCP: - return "TCP"; + CASE("TCP"); case IPPROTO_UDP: - return "UDP"; + CASE("UDP"); case IPPROTO_ICMPV6: - return "ICMPv6"; + CASE("ICMPv6"); default: - return ""; + CASE(""); +#undef CASE } } diff --git a/ip.h b/ip.h index f6c29e00..aab9b86a 100644 --- a/ip.h +++ b/ip.h @@ -117,6 +117,7 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h) ip6h->flow_lbl[2]; } +#define IPPROTO_STRLEN (sizeof("")) const char *ipproto_name(uint8_t proto); /* IPv6 link-local all-nodes multicast address, ff02::1 */ -- 2.53.0