summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-10-26 16:47:31 +0100
committerThomas Haller <thaller@redhat.com>2020-10-27 17:04:20 +0100
commitf5b89e8060ac718d971b5de97a78c60930b07b95 (patch)
tree0cc8a51b92b87099698c2424fb60696d5827bb0f
parent931573dfda5d009b20800d6155a5b2b53fc8bb75 (diff)
downloadNetworkManager-f5b89e8060ac718d971b5de97a78c60930b07b95.tar.gz
shared: add nm_mult_clamped_u() helper
-rw-r--r--shared/nm-std-aux/nm-std-aux.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h
index a10abfb6a6..7b06472631 100644
--- a/shared/nm-std-aux/nm-std-aux.h
+++ b/shared/nm-std-aux/nm-std-aux.h
@@ -202,6 +202,24 @@ nm_add_clamped_u32(uint32_t a, uint32_t b)
return c;
}
+static inline unsigned
+nm_mult_clamped_u(unsigned a, unsigned b)
+{
+ unsigned c;
+
+ /* returns a*b, or UINT_MAX if the result would overflow. */
+
+ if (b == 0)
+ return 0;
+
+ c = a * b;
+
+ if (c / b != a)
+ return (unsigned) -1;
+
+ return c;
+}
+
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*