2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_OVERFLOWS_H
|
|
|
|
#define LIGHTNING_COMMON_OVERFLOWS_H
|
2016-01-21 21:08:08 +01:00
|
|
|
#include "config.h"
|
2015-05-28 23:38:27 +02:00
|
|
|
|
|
|
|
static inline bool add_overflows_size_t(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
return (size_t)a != a || (size_t)b != b || (a + b) < (size_t)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool add_overflows_u64(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
return (a + b) < a;
|
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
static inline bool mul_overflows_s64(int64_t a, int64_t b)
|
|
|
|
{
|
|
|
|
int64_t ret;
|
|
|
|
|
|
|
|
if (a == 0)
|
|
|
|
return false;
|
|
|
|
ret = a * b;
|
|
|
|
return (ret / a != b);
|
|
|
|
}
|
2017-04-27 08:08:50 +02:00
|
|
|
|
|
|
|
static inline bool mul_overflows_u64(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
uint64_t ret;
|
|
|
|
|
|
|
|
if (a == 0)
|
|
|
|
return false;
|
|
|
|
ret = a * b;
|
|
|
|
return (ret / a != b);
|
|
|
|
}
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_OVERFLOWS_H */
|