From 75f0e66f1ffde0ca8c9b8d2fbbd7b2981951a300 Mon Sep 17 00:00:00 2001 From: trainstopperd Date: Wed, 6 Sep 2023 23:25:37 -0400 Subject: [PATCH] feat: unsigned add checks --- src/liberror/public.h | 27 ++++++++++++++++++++++++++- tests/{compile => }/assert.c | 0 tests/{compile => }/test.c | 0 3 files changed, 26 insertions(+), 1 deletion(-) rename tests/{compile => }/assert.c (100%) rename tests/{compile => }/test.c (100%) 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