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=Av5dIcoC; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 02A955A061E for ; Thu, 19 Mar 2026 07:12:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1773900722; bh=dDRzaTEOVtQgnc4HEsLm2goStyMtr2zYyzC6ofWslxM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Av5dIcoCEQnT/r2BRBSx+AjYlwxChIKmiiLKOfCwtwkXcP9NGfz7rlxz6SJE6b+Zp uccwStXMpeGc8K0DJv5xWSw2e3h5mJ71Qw9MD7iJScaJNKHL0Ib8cVCbCCfPk4e0tD l6M2pMJlnNJdzFle0FKWDuXhh2HWSKJIIW+zVs1kzUhyFLOWjtXhnmuPfzsfp9N/QQ sx5KJuAWBWCNPlgXZwlkEgLfeQCSPYDl/kiBtCAgTdGsdRvr4EtcOplM0ZiCcevMjF 7gzb6CBt06h9YG/bp3ZgJAPpqIEyJ4IgjRYciuuV/5mqywm7TS9rdnJENNsOALiYEk kquB3PEUyzFpA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fbwMQ160Qz4wSx; Thu, 19 Mar 2026 17:12:02 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 13/15] ip: Define a bound for the string returned by ipproto_name() Date: Thu, 19 Mar 2026 17:11:55 +1100 Message-ID: <20260319061157.1983818-14-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260319061157.1983818-1-david@gibson.dropbear.id.au> References: <20260319061157.1983818-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: PUESJPCWLZZ7UA6L6KLVIJQIC6TZHKNE X-Message-ID-Hash: PUESJPCWLZZ7UA6L6KLVIJQIC6TZHKNE 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