glib-aux: add assertions for valid prefix length

(cherry picked from commit 9ce4a16523)
(cherry picked from commit 0180a9fca5)
This commit is contained in:
Thomas Haller 2022-04-05 18:25:24 +02:00
parent 2e6d70e51f
commit b7a5d41ffe
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 4 additions and 7 deletions

View file

@ -6320,9 +6320,10 @@ nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
int nbytes;
guint8 va, vb, m;
if (plen >= 128)
if (plen >= 128) {
nm_assert(plen == 128);
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, sizeof(struct in6_addr));
else {
} else {
nbytes = plen / 8;
if (nbytes)
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, nbytes);

View file

@ -398,6 +398,7 @@ gboolean nm_utils_get_ipv6_interface_identifier(NMLinkType link_type,
static inline in_addr_t
_nm_utils_ip4_prefix_to_netmask(guint32 prefix)
{
nm_assert(prefix <= 32);
return prefix < 32 ? ~htonl(0xFFFFFFFFu >> prefix) : 0xFFFFFFFFu;
}
@ -455,8 +456,6 @@ nm_utils_ip_address_same_prefix_cmp(int addr_family,
gconstpointer addr_b,
guint8 plen)
{
nm_assert_addr_family(addr_family);
NM_CMP_SELF(addr_a, addr_b);
if (NM_IS_IPv4(addr_family)) {

View file

@ -283,9 +283,6 @@ test_nm_utils_ip4_prefix_to_netmask(void)
g_assert_cmpint(_nm_utils_ip4_prefix_to_netmask(32),
==,
nmtst_inet4_from_string("255.255.255.255"));
g_assert_cmpint(_nm_utils_ip4_prefix_to_netmask(33),
==,
nmtst_inet4_from_string("255.255.255.255"));
}
/*****************************************************************************/