summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2022-06-23 07:35:13 +0200
committerThomas Haller <thaller@redhat.com>2022-06-27 10:50:13 +0200
commit7a33870bf13527186a2e9ed5fdfd5486b9fc15d1 (patch)
tree187305b516b4f29ce8f6e938d1775fdf5da82dfe
parentc48a312fc7cbd4bf5c24c4d196ebc9476b4388d4 (diff)
libnm: assert nm_utils_ip4_prefix_to_netmask() for valid IPv4 prefix length
There was already an nm_assert() assertion. Upgrade this to a g_return_val_if_fail(). This function is public API, so this is potentially an API break. But it should highlight a bug in the caller.
-rw-r--r--src/libnm-core-impl/nm-utils.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/libnm-core-impl/nm-utils.c b/src/libnm-core-impl/nm-utils.c
index 0d3f6a6fda..d1590a7a54 100644
--- a/src/libnm-core-impl/nm-utils.c
+++ b/src/libnm-core-impl/nm-utils.c
@@ -1558,7 +1558,13 @@ nm_utils_ip4_routes_from_variant(GVariant *value)
/**
* nm_utils_ip4_netmask_to_prefix:
- * @netmask: an IPv4 netmask in network byte order
+ * @netmask: an IPv4 netmask in network byte order.
+ * Usually the netmask has all leading bits up to the prefix
+ * set so that the netmask is identical to having the first
+ * prefix bits of the address set.
+ * If that is not the case and there are "holes" in the
+ * mask, the prefix is determined based on the lowest bit
+ * set.
*
* Returns: the CIDR prefix represented by the netmask
**/
@@ -1567,6 +1573,7 @@ nm_utils_ip4_netmask_to_prefix(guint32 netmask)
{
G_STATIC_ASSERT_EXPR(__SIZEOF_INT__ == 4);
G_STATIC_ASSERT_EXPR(sizeof(int) == 4);
+ G_STATIC_ASSERT_EXPR(sizeof(guint) == 4);
G_STATIC_ASSERT_EXPR(sizeof(netmask) == 4);
return ((netmask != 0u) ? (guint32) (32 - __builtin_ctz(ntohl(netmask))) : 0u);
@@ -1574,13 +1581,15 @@ nm_utils_ip4_netmask_to_prefix(guint32 netmask)
/**
* nm_utils_ip4_prefix_to_netmask:
- * @prefix: a CIDR prefix
+ * @prefix: a CIDR prefix, must be not larger than 32.
*
* Returns: the netmask represented by the prefix, in network byte order
**/
guint32
nm_utils_ip4_prefix_to_netmask(guint32 prefix)
{
+ g_return_val_if_fail(prefix <= 32, 0xffffffffu);
+
return _nm_utils_ip4_prefix_to_netmask(prefix);
}