summaryrefslogtreecommitdiffstats
path: root/src/fundamental
diff options
context:
space:
mode:
authorAdrian Vovk <adrianvovk@gmail.com>2024-01-17 20:48:45 +0100
committerAdrian Vovk <adrianvovk@gmail.com>2024-02-13 19:08:20 +0100
commita7a67dfd9d1da1c8027b58e691663a02e904987f (patch)
treebe3be93e64c6148066b59c9f167b2971cbcfd6c2 /src/fundamental
parentUse tilde for rc tag versioning (diff)
downloadsystemd-a7a67dfd9d1da1c8027b58e691663a02e904987f.tar.xz
systemd-a7a67dfd9d1da1c8027b58e691663a02e904987f.zip
fundamental: Add overflow-safe math helpers
ADD_SAFE/SUB_SAFE/MUL_SAFE do addition/subtraction/multiplication respectively with an overflow check. If an overflow occurs these return false, otherwise true. Example: (c = a + b) would become ADD_SAFE(&c, a, b) INC_SAFE/DEC_SAFE/MUL_ASSIGN_SAFE are like above but they also reassign the first argument. Example: (a += b) would become INC_SAFE(&a, b)
Diffstat (limited to 'src/fundamental')
-rw-r--r--src/fundamental/macro-fundamental.h26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h
index 041649a03b..5ccbda5186 100644
--- a/src/fundamental/macro-fundamental.h
+++ b/src/fundamental/macro-fundamental.h
@@ -249,6 +249,30 @@
CONST_ISPOWEROF2(_x); \
}))
+#define ADD_SAFE(ret, a, b) (!__builtin_add_overflow(a, b, ret))
+#define INC_SAFE(a, b) __INC_SAFE(UNIQ, a, b)
+#define __INC_SAFE(q, a, b) \
+ ({ \
+ const typeof(a) UNIQ_T(A, q) = (a); \
+ ADD_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \
+ })
+
+#define SUB_SAFE(ret, a, b) (!__builtin_sub_overflow(a, b, ret))
+#define DEC_SAFE(a, b) __DEC_SAFE(UNIQ, a, b)
+#define __DEC_SAFE(q, a, b) \
+ ({ \
+ const typeof(a) UNIQ_T(A, q) = (a); \
+ SUB_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \
+ })
+
+#define MUL_SAFE(ret, a, b) (!__builtin_mul_overflow(a, b, ret))
+#define MUL_ASSIGN_SAFE(a, b) __MUL_ASSIGN_SAFE(UNIQ, a, b)
+#define __MUL_ASSIGN_SAFE(q, a, b) \
+ ({ \
+ const typeof(a) UNIQ_T(A, q) = (a); \
+ MUL_SAFE(UNIQ_T(A, q), *UNIQ_T(A, q), b); \
+ })
+
#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
#define __LESS_BY(aq, a, bq, b) \
({ \
@@ -298,7 +322,7 @@
const typeof(y) UNIQ_T(A, q) = (y); \
const typeof(x) UNIQ_T(B, q) = DIV_ROUND_UP((x), UNIQ_T(A, q)); \
typeof(x) UNIQ_T(C, q); \
- __builtin_mul_overflow(UNIQ_T(B, q), UNIQ_T(A, q), &UNIQ_T(C, q)) ? (typeof(x)) -1 : UNIQ_T(C, q); \
+ MUL_SAFE(&UNIQ_T(C, q), UNIQ_T(B, q), UNIQ_T(A, q)) ? UNIQ_T(C, q) : (typeof(x)) -1; \
})
#define ROUND_UP(x, y) __ROUND_UP(UNIQ, (x), (y))