8#ifndef BOTAN_UTILS_SAFE_INT_H_
9#define BOTAN_UTILS_SAFE_INT_H_
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
25 Exception(
fmt(
"Integer overflow detected at {}:{}", file, line)) {}
30inline size_t checked_add(
size_t x,
size_t y,
const char* file,
int line) {
31#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_add_overflow)
33 if(__builtin_add_overflow(x, y, &z)) [[unlikely]]
34#elif defined(_MSC_VER)
36 if(SizeTAdd(x, y, &z) != S_OK) [[unlikely]]
39 if(z < x) [[unlikely]]
48#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_add_overflow)
50 if(__builtin_mul_overflow(x, y, &z)) [[unlikely]]
51#elif defined(_MSC_VER)
53 if(SizeTMult(x, y, &z) != S_OK) [[unlikely]]
56 if(y && z / y != x) [[unlikely]]
64template <
typename RT,
typename AT>
66 RT c =
static_cast<RT
>(i);
67 if(i !=
static_cast<AT
>(c)) {
73#define BOTAN_CHECKED_ADD(x, y) checked_add(x, y, __FILE__, __LINE__)
74#define BOTAN_CHECKED_MUL(x, y) checked_mul(x, y)
Integer_Overflow_Detected(std::string_view file, int line)
ErrorType error_type() const noexcept override
int(* final)(unsigned char *, CTX *)
std::optional< size_t > checked_mul(size_t x, size_t y)
std::string fmt(std::string_view format, const T &... args)
size_t checked_add(size_t x, size_t y, const char *file, int line)