From 8a3310043b15ba861b6821d005decf665784a9e8 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 21 Jan 2021 08:14:37 +0100 Subject: [PATCH] ndisc: add static asserts to _route_preference_coerce() Our internal NMIcmpv6RouterPref defines must be numerically identical to the values in the protocol. Add a static assertion for that. --- src/ndisc/nm-lndp-ndisc.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ndisc/nm-lndp-ndisc.c b/src/ndisc/nm-lndp-ndisc.c index 1d8becbfe..6f9ac9dca 100644 --- a/src/ndisc/nm-lndp-ndisc.c +++ b/src/ndisc/nm-lndp-ndisc.c @@ -85,14 +85,26 @@ send_rs(NMNDisc *ndisc, GError **error) static NMIcmpv6RouterPref _route_preference_coerce(enum ndp_route_preference pref) { +#define _ASSERT_ENUM(v1, v2) \ + G_STMT_START \ + { \ + G_STATIC_ASSERT((NMIcmpv6RouterPref)(v1) == (v2)); \ + G_STATIC_ASSERT((enum ndp_route_preference)(v2) == (v1)); \ + G_STATIC_ASSERT((gint64)(v1) == (v2)); \ + G_STATIC_ASSERT((gint64)(v2) == (v1)); \ + } \ + G_STMT_END + switch (pref) { case NDP_ROUTE_PREF_LOW: - return NM_ICMPV6_ROUTER_PREF_LOW; case NDP_ROUTE_PREF_MEDIUM: - return NM_ICMPV6_ROUTER_PREF_MEDIUM; case NDP_ROUTE_PREF_HIGH: - return NM_ICMPV6_ROUTER_PREF_HIGH; + _ASSERT_ENUM(NDP_ROUTE_PREF_LOW, NM_ICMPV6_ROUTER_PREF_LOW); + _ASSERT_ENUM(NDP_ROUTE_PREF_MEDIUM, NM_ICMPV6_ROUTER_PREF_MEDIUM); + _ASSERT_ENUM(NDP_ROUTE_PREF_HIGH, NM_ICMPV6_ROUTER_PREF_HIGH); + return (NMIcmpv6RouterPref) pref; } + /* unexpected value must be treated as MEDIUM (RFC 4191). */ return NM_ICMPV6_ROUTER_PREF_MEDIUM; }