feat: unsigned add checks

trunk
trainstopperd 2023-09-06 23:25:37 -04:00
parent a2f2903f77
commit 75f0e66f1f
3 changed files with 26 additions and 1 deletions

View File

@ -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