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"
|
2021-07-11 08:59:42 +02:00
|
|
|
#include <ccan/short_types/short_types.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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2021-07-11 08:59:42 +02:00
|
|
|
|
|
|
|
static inline bool assign_overflow_u8(u8 *dst, uint64_t v)
|
|
|
|
{
|
|
|
|
*dst = v;
|
|
|
|
return *dst == v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool assign_overflow_u16(u16 *dst, uint64_t v)
|
|
|
|
{
|
|
|
|
*dst = v;
|
|
|
|
return *dst == v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool assign_overflow_u32(u32 *dst, uint64_t v)
|
|
|
|
{
|
|
|
|
*dst = v;
|
|
|
|
return *dst == v;
|
|
|
|
}
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_OVERFLOWS_H */
|