| 1 |
/// @file |
| 2 |
/// @brief arithmetic overflow helpers |
| 3 |
/// @ingroup cgraph_utils |
| 4 |
/// |
| 5 |
/// Replace this with stdckdint.h when moving to C23. |
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <limits.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <stddef.h> |
| 13 |
#include <stdint.h> |
| 14 |
|
| 15 |
/** add two integers, checking for overflow |
| 16 |
* |
| 17 |
* @param a Operand 1 |
| 18 |
* @param b Operand 2 |
| 19 |
* @param res [out] Result on success |
| 20 |
* @return True if overflow would occur |
| 21 |
*/ |
| 22 |
static inline bool sadd_overflow(int a, int b, int *res) { |
| 23 |
assert(res != NULL); |
| 24 |
|
| 25 |
// delegate to hardware optimized implementations where possible |
| 26 |
#if defined(__clang__) && \ |
| 27 |
(__clang_major__ > 3 || \ |
| 28 |
(__clang_major__ == 3 && __clang_minor__ > 7)) // Clang ≥ 3.8 |
| 29 |
return __builtin_sadd_overflow(a, b, res); |
| 30 |
#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5 |
| 31 |
return __builtin_sadd_overflow(a, b, res); |
| 32 |
#endif |
| 33 |
|
| 34 |
if (a > 0 && INT_MAX - a < b) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
if (a < 0 && INT_MIN - a > b) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
*res = a + b; |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
/// add two sizes, checking for overflow |
| 46 |
/// |
| 47 |
/// @param a Operand 1 |
| 48 |
/// @param b Operand 2 |
| 49 |
/// @param res [out] Result on success |
| 50 |
/// @return True if overflow would occur |
| 51 |
static inline bool size_overflow(size_t a, size_t b, size_t *res) { |
| 52 |
assert(res != NULL); |
| 53 |
|
| 54 |
// delegate to hardware optimized implementations where possible |
| 55 |
#if defined(__clang__) && \ |
| 56 |
(__clang_major__ > 3 || \ |
| 57 |
(__clang_major__ == 3 && __clang_minor__ > 7)) // Clang ≥ 3.8 |
| 58 |
return __builtin_add_overflow(a, b, res); |
| 59 |
#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5 |
| 60 |
return __builtin_add_overflow(a, b, res); |
| 61 |
#endif |
| 62 |
|
| 63 |
if (SIZE_MAX - a < b) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
*res = a + b; |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/// add two 64-bit unsigned integers, checking for overflow |
| 72 |
/// |
| 73 |
/// @param a Operand 1 |
| 74 |
/// @param b Operand 2 |
| 75 |
/// @param res [out] Result on success |
| 76 |
/// @return True if overflow would occur |
| 77 |
static inline bool u64add_overflow(uint64_t a, uint64_t b, uint64_t *res) { |
| 78 |
assert(res != NULL); |
| 79 |
|
| 80 |
// delegate to hardware optimized implementations where possible |
| 81 |
#if defined(__clang__) && \ |
| 82 |
(__clang_major__ > 3 || \ |
| 83 |
(__clang_major__ == 3 && __clang_minor__ > 7)) // Clang ≥ 3.8 |
| 84 |
return __builtin_add_overflow(a, b, res); |
| 85 |
#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5 |
| 86 |
return __builtin_add_overflow(a, b, res); |
| 87 |
#endif |
| 88 |
|
| 89 |
if (UINT64_MAX - a < b) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
*res = a + b; |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
/// multiply two 64-bit unsigned integers, checking for overflow |
| 98 |
/// |
| 99 |
/// @param a Operand 1 |
| 100 |
/// @param b Operand 2 |
| 101 |
/// @param res [out] Result on success |
| 102 |
/// @return True if overflow would occur |
| 103 |
static inline bool u64mul_overflow(uint64_t a, uint64_t b, uint64_t *res) { |
| 104 |
assert(res != NULL); |
| 105 |
|
| 106 |
// delegate to hardware optimized implementations where possible |
| 107 |
#if defined(__clang__) && \ |
| 108 |
(__clang_major__ > 3 || \ |
| 109 |
(__clang_major__ == 3 && __clang_minor__ > 7)) // Clang ≥ 3.8 |
| 110 |
return __builtin_mul_overflow(a, b, res); |
| 111 |
#elif defined(__GNUC__) && __GNUC__ > 4 // GCC ≥ 5 |
| 112 |
return __builtin_mul_overflow(a, b, res); |
| 113 |
#endif |
| 114 |
|
| 115 |
if (a > 0 && UINT64_MAX / a < b) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
*res = a * b; |
| 120 |
return false; |
| 121 |
} |