diff options
| author | David Gibson <david@gibson.dropbear.id.au> | 2026-03-27 15:34:28 +1100 |
|---|---|---|
| committer | Stefano Brivio <sbrivio@redhat.com> | 2026-03-28 14:36:01 +0100 |
| commit | 93c3e351f23561331824f2a2305bde3628676db5 (patch) | |
| tree | 77fdb0c4b9a06f21fa4c7b371f5c79f3631d88df | |
| parent | ed187c353dffb0362941e1612d41307e7bff1783 (diff) | |
| download | passt-93c3e351f23561331824f2a2305bde3628676db5.tar passt-93c3e351f23561331824f2a2305bde3628676db5.tar.gz passt-93c3e351f23561331824f2a2305bde3628676db5.tar.bz2 passt-93c3e351f23561331824f2a2305bde3628676db5.tar.lz passt-93c3e351f23561331824f2a2305bde3628676db5.tar.xz passt-93c3e351f23561331824f2a2305bde3628676db5.tar.zst passt-93c3e351f23561331824f2a2305bde3628676db5.zip | |
ip: Define a bound for the string returned by ipproto_name()
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 <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
| -rw-r--r-- | ip.c | 18 | ||||
| -rw-r--r-- | ip.h | 2 |
2 files changed, 14 insertions, 6 deletions
@@ -12,6 +12,7 @@ * Author: Stefano Brivio <sbrivio@redhat.com> */ +#include <assert.h> #include <stddef.h> #include <netinet/in.h> @@ -74,7 +75,7 @@ found: * 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 @@ -83,16 +84,21 @@ found: 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 "<unknown protocol>"; + CASE("<unknown protocol>"); +#undef CASE } } @@ -118,6 +118,8 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h) } bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen); + +#define IPPROTO_STRLEN (sizeof("<unknown protocol>")) const char *ipproto_name(uint8_t proto); /* IPv6 link-local all-nodes multicast address, ff02::1 */ |
