From 0cf9db42d4a40e8602d86759d75bc25b0f84f313 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 5 Apr 2022 18:12:24 +0200 Subject: [PATCH] glib-aux: use uint32 type for prefix length parameter Of course, the prefix length cannot be larger than 32 or 128. But as C does implicit conversions, a buggy prefix length can lead to a (wrongly) valid prefix length. Make the type uint32, to prevent that (at least for common cases, unless you pass a huge 64 bit integer). --- src/libnm-glib-aux/nm-shared-utils.c | 9 +++++---- src/libnm-glib-aux/nm-shared-utils.h | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index 304740d60e..9c7bceb01b 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -6282,7 +6282,7 @@ _nm_utils_ssid_to_string_gbytes(GBytes *ssid) /*****************************************************************************/ gconstpointer -nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen) +nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint32 plen) { g_return_val_if_fail(dst, NULL); @@ -6308,7 +6308,8 @@ nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer /* nm_utils_ip6_address_clear_host_address: * @dst: destination output buffer, will contain the network part of the @src address - * @src: source ip6 address + * @src: source ip6 address. If NULL, this does an in-place update of @dst. + * Also, @src and @dst are allowed to be the same pointers. * @plen: prefix length of network * * Note: this function is self assignment safe, to update @src inplace, set both @@ -6317,7 +6318,7 @@ nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer const struct in6_addr * nm_utils_ip6_address_clear_host_address(struct in6_addr *dst, const struct in6_addr *src, - guint8 plen) + guint32 plen) { g_return_val_if_fail(plen <= 128, NULL); g_return_val_if_fail(dst, NULL); @@ -6346,7 +6347,7 @@ nm_utils_ip6_address_clear_host_address(struct in6_addr *dst, int nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a, const struct in6_addr *addr_b, - guint8 plen) + guint32 plen) { int nbytes; guint8 va, vb, m; diff --git a/src/libnm-glib-aux/nm-shared-utils.h b/src/libnm-glib-aux/nm-shared-utils.h index 0953063081..56ab5e0d93 100644 --- a/src/libnm-glib-aux/nm-shared-utils.h +++ b/src/libnm-glib-aux/nm-shared-utils.h @@ -405,7 +405,7 @@ guint32 _nm_utils_ip4_get_default_prefix0(in_addr_t ip); guint32 _nm_utils_ip4_get_default_prefix(in_addr_t ip); gconstpointer -nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen); +nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint32 plen); /* nm_utils_ip4_address_clear_host_address: * @addr: source ip6 address @@ -414,17 +414,17 @@ nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer * returns: the input address, with the host address set to 0. */ static inline in_addr_t -nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen) +nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint32 plen) { return addr & _nm_utils_ip4_prefix_to_netmask(plen); } const struct in6_addr *nm_utils_ip6_address_clear_host_address(struct in6_addr *dst, const struct in6_addr *src, - guint8 plen); + guint32 plen); static inline int -nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint8 plen) +nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint32 plen) { NM_CMP_DIRECT(htonl(nm_utils_ip4_address_clear_host_address(addr_a, plen)), htonl(nm_utils_ip4_address_clear_host_address(addr_b, plen))); @@ -433,10 +433,10 @@ nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint8 int nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a, const struct in6_addr *addr_b, - guint8 plen); + guint32 plen); static inline gboolean -nm_utils_ip4_address_same_prefix(in_addr_t addr_a, in_addr_t addr_b, guint8 plen) +nm_utils_ip4_address_same_prefix(in_addr_t addr_a, in_addr_t addr_b, guint32 plen) { return nm_utils_ip4_address_same_prefix_cmp(addr_a, addr_b, plen) == 0; }