feat: unsigned add checks
parent
a2f2903f77
commit
75f0e66f1f
|
@ -1,7 +1,7 @@
|
||||||
#ifndef CWARE_LIBERROR_PUBLIC_H
|
#ifndef CWARE_LIBERROR_PUBLIC_H
|
||||||
#define 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) \
|
#define LIBERROR_ASSERT(expression, type, message, function) \
|
||||||
do { \
|
do { \
|
||||||
if((expression) != 0) { \
|
if((expression) != 0) { \
|
||||||
|
@ -15,10 +15,35 @@
|
||||||
#define LIBERROR_TEST(expression, message) \
|
#define LIBERROR_TEST(expression, message) \
|
||||||
LIBERROR_ASSERT(expression, "ASSERTION", message, "main")
|
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
|
#else
|
||||||
|
|
||||||
#define LIBERROR_ASSERT(expression, message)
|
#define LIBERROR_ASSERT(expression, message)
|
||||||
#define LIBERROR_TEST(expression, type, message, function)
|
#define LIBERROR_TEST(expression, type, message, function)
|
||||||
|
#define LIBERROR_UNSIGNED_ADD_CHECk(a, b, type, message, function)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue