overflows.h (864B)
1 #ifndef LIGHTNING_COMMON_OVERFLOWS_H 2 #define LIGHTNING_COMMON_OVERFLOWS_H 3 #include "config.h" 4 #include "short_types.h" 5 6 static inline bool add_overflows_size_t(uint64_t a, uint64_t b) 7 { 8 return (size_t)a != a || (size_t)b != b || (a + b) < (size_t)a; 9 } 10 11 static inline bool add_overflows_u64(uint64_t a, uint64_t b) 12 { 13 return (a + b) < a; 14 } 15 16 static inline bool mul_overflows_u64(uint64_t a, uint64_t b) 17 { 18 uint64_t ret; 19 20 if (a == 0) 21 return false; 22 ret = a * b; 23 return (ret / a != b); 24 } 25 26 static inline bool assign_overflow_u8(u8 *dst, uint64_t v) 27 { 28 *dst = v; 29 return *dst == v; 30 } 31 32 static inline bool assign_overflow_u16(u16 *dst, uint64_t v) 33 { 34 *dst = v; 35 return *dst == v; 36 } 37 38 static inline bool assign_overflow_u32(u32 *dst, uint64_t v) 39 { 40 *dst = (u32)v; 41 return *dst == v; 42 } 43 #endif /* LIGHTNING_COMMON_OVERFLOWS_H */