dovecot-2.2: lib: Added UINT64_SUM_OVERFLOWS()

dovecot at dovecot.org dovecot at dovecot.org
Wed Jul 2 17:38:30 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/d77d880c1385
changeset: 17564:d77d880c1385
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jul 02 20:13:35 2014 +0300
description:
lib: Added UINT64_SUM_OVERFLOWS()
Maybe the unit tests are kind of unnecessary since the macro is so simple,
but at least it's now a well tested simple macro :)

diffstat:

 src/lib/bits.h      |   3 +++
 src/lib/test-bits.c |  24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 0 deletions(-)

diffs (49 lines):

diff -r 2ed2ab04b63d -r d77d880c1385 src/lib/bits.h
--- a/src/lib/bits.h	Wed Jul 02 18:21:24 2014 +0300
+++ b/src/lib/bits.h	Wed Jul 02 20:13:35 2014 +0300
@@ -12,6 +12,9 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#define UINT64_SUM_OVERFLOWS(a, b) \
+	(a > (uint64_t)-1 - b)
+
 size_t nearest_power(size_t num) ATTR_CONST;
 
 unsigned int bits_required8(uint8_t num) ATTR_CONST;
diff -r 2ed2ab04b63d -r d77d880c1385 src/lib/test-bits.c
--- a/src/lib/test-bits.c	Wed Jul 02 18:21:24 2014 +0300
+++ b/src/lib/test-bits.c	Wed Jul 02 20:13:35 2014 +0300
@@ -60,8 +60,32 @@
 	test_end();
 }
 
+static void test_sum_overflows(void)
+{
+#define MAX64 (uint64_t)-1
+	static const struct {
+		uint64_t a, b;
+		bool overflows;
+	} tests[] = {
+		{ MAX64-1, 1, FALSE },
+		{ MAX64, 1, TRUE },
+		{ MAX64-1, 1, FALSE },
+		{ MAX64-1, 2, TRUE },
+		{ MAX64-1, MAX64-1, TRUE },
+		{ MAX64-1, MAX64, TRUE },
+		{ MAX64, MAX64, TRUE }
+	};
+	unsigned int i;
+
+	test_begin("UINT64_SUM_OVERFLOWS");
+	for (i = 0; i < N_ELEMENTS(tests); i++)
+		test_assert(UINT64_SUM_OVERFLOWS(tests[i].a, tests[i].b) == tests[i].overflows);
+	test_end();
+}
+
 void test_bits()
 {
 	test_nearest_power();
 	test_bits_requiredXX();
+	test_sum_overflows();
 }


More information about the dovecot-cvs mailing list