| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_COMMON_SAFE_H |
| 8 | #define BABELTRACE_COMMON_SAFE_H |
| 9 | |
| 10 | #include <stdbool.h> |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | static inline |
| 18 | bool bt_safe_to_mul_int64(int64_t a, int64_t b) |
| 19 | { |
| 20 | if (a == 0 || b == 0) { |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | return a < INT64_MAX / b; |
| 25 | } |
| 26 | |
| 27 | static inline |
| 28 | bool bt_safe_to_mul_uint64(uint64_t a, uint64_t b) |
| 29 | { |
| 30 | if (a == 0 || b == 0) { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | return a < UINT64_MAX / b; |
| 35 | } |
| 36 | |
| 37 | static inline |
| 38 | bool bt_safe_to_add_int64(int64_t a, int64_t b) |
| 39 | { |
| 40 | return a <= INT64_MAX - b; |
| 41 | } |
| 42 | |
| 43 | static inline |
| 44 | bool bt_safe_to_add_uint64(uint64_t a, uint64_t b) |
| 45 | { |
| 46 | return a <= UINT64_MAX - b; |
| 47 | } |
| 48 | |
| 49 | #ifdef __cplusplus |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | #endif /* BABELTRACE_COMMON_SAFE_H */ |