diff --git a/src/liberror/public.h b/src/liberror/public.h index e74a2b3..71d4897 100755 --- a/src/liberror/public.h +++ b/src/liberror/public.h @@ -1,7 +1,7 @@ #ifndef CWARE_LIBERROR_PUBLIC_H #define CWARE_LIBERROR_PUBLIC_H -#if !defined(LIBERROR_NO_SAFETY_CHECKS) +#ifndef LIBERROR_NO_SAFETY_CHECKS #define LIBERROR_ASSERT(expression, type, message, function) \ do { \ if((expression) != 0) { \ @@ -15,10 +15,35 @@ #define LIBERROR_TEST(expression, message) \ LIBERROR_ASSERT(expression, "ASSERTION", message, "main") +#define LIBERROR_UNSIGNED_ADD_CHECK(a, b, type, message, function) \ + do { \ + /* a + b, where either a or b are zero, can never overflow. */ \ + if(((a) == 0) || ((b) == 0)) { \ + break; \ + } \ + \ + if((a) > (b)) { \ + /* a + b is greater or equal to a, so there was no overflow. */ \ + if(((a) + (b)) >= (a)) { \ + break; \ + } \ + \ + LIBERROR_ASSERT(1 == 0, type, message, function); \ + } else if((a) <= (b)) { \ + /* a + b is greater or equal to b, so there was no overflow. */ \ + if(((a) + (b)) >= (b)) { \ + break; \ + } \ + \ + LIBERROR_ASSERT(1 == 0, type, message, function); + } while(0) + #else #define LIBERROR_ASSERT(expression, message) #define LIBERROR_TEST(expression, type, message, function) +#define LIBERROR_UNSIGNED_ADD_CHECk(a, b, type, message, function) + #endif #endif diff --git a/tests/compile/assert.c b/tests/assert.c similarity index 100% rename from tests/compile/assert.c rename to tests/assert.c diff --git a/tests/compile/test.c b/tests/test.c similarity index 100% rename from tests/compile/test.c rename to tests/test.c