summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2022-07-28 18:38:42 +0200
committerThomas Haller <thaller@redhat.com>2022-08-09 08:02:36 +0200
commit49ddb96d4ef78e5289e64bbf9f0d257c7df25873 (patch)
tree479bd7bd465c4168ca689330bcf50cac3746fe36
parent0a26320c091a9282134186a0fd58be784051d415 (diff)
downloadNetworkManager-49ddb96d4ef78e5289e64bbf9f0d257c7df25873.tar.gz
std-aux: add nm_utils_addr_family_to_size_untrusted() helper
-rw-r--r--src/libnm-std-aux/nm-std-aux.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h
index 18c467b3b3..77243ab186 100644
--- a/src/libnm-std-aux/nm-std-aux.h
+++ b/src/libnm-std-aux/nm-std-aux.h
@@ -1307,6 +1307,28 @@ nm_utils_addr_family_to_size(int addr_family)
return nm_assert_unreachable_val(0);
}
+static inline size_t
+nm_utils_addr_family_to_size_untrusted(int addr_family)
+{
+ /* This is almost the same as nm_utils_addr_family_to_size().
+ * The difference is that nm_utils_addr_family_to_size() requires that
+ * addr_family is either AF_INET or AF_INET6 (it asserts against that).
+ *
+ * This variant accepts any addr_family, but returns zero for any unknown
+ * family.
+ *
+ * Use this, if the address family is untrusted or not guaranteed to be valid.
+ * Of course, then you also need to handle that this function potentially returns
+ * zero. */
+ switch (addr_family) {
+ case NM_AF_INET:
+ return NM_AF_INET_SIZE;
+ case NM_AF_INET6:
+ return NM_AF_INET6_SIZE;
+ }
+ return 0;
+}
+
static inline int
nm_utils_addr_family_from_size(size_t len)
{