summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-10-22 10:22:48 +0200
committerThomas Haller <thaller@redhat.com>2020-10-23 17:11:53 +0200
commitb38075b751710a1d954188e595e65749d67abf29 (patch)
tree8c87b20ba97e861e150aaddd92d27efff24297a4
parentc3fe895ebcca12339f8ce92be2c961bdd1f33e6b (diff)
shared: add nm_add_u32_clamped() helper
-rw-r--r--shared/nm-std-aux/nm-std-aux.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h
index 762f104be..b181f9382 100644
--- a/shared/nm-std-aux/nm-std-aux.h
+++ b/shared/nm-std-aux/nm-std-aux.h
@@ -189,6 +189,19 @@ typedef uint64_t _nm_bitwise nm_be64_t;
/*****************************************************************************/
+static inline uint32_t
+nm_add_u32_clamped(uint32_t a, uint32_t b)
+{
+ uint32_t c;
+
+ /* returns the sum of a+b, or UINT32_MAX if the result would overflow. */
+
+ c = a + b;
+ if (c < a)
+ return UINT32_MAX;
+ return c;
+}
+
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*