From 8c6ad1b002a2212e8d024a202b435127affbc623 Mon Sep 17 00:00:00 2001 From: Nicolas F Date: Sat, 13 Jun 2026 20:04:01 +0200 Subject: [PATCH] osdep/compiler: add MP_CKD_MUL Add a new MP_CKD_MUL macro which does a multiplication with overflow checking, backed by either the C23 ckd_mul standard function, or the GCC/Clang __builtin_mul_overflow otherwise. --- osdep/compiler.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/osdep/compiler.h b/osdep/compiler.h index 07cdd147d7..555d3d141b 100644 --- a/osdep/compiler.h +++ b/osdep/compiler.h @@ -7,6 +7,7 @@ #define MP_EXPAND_ARGS(...) __VA_ARGS__ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#include #define MP_NORETURN [[noreturn]] #define MP_FALLTHROUGH [[fallthrough]] #define MP_WARN_UNUSED_RESULT [[nodiscard]] @@ -84,4 +85,10 @@ #define MP_SCANF_ATTRIBUTE(a1, a2) #endif +#if defined(__STDC_VERSION_STDCKDINT_H__) && __STDC_VERSION_STDCKDINT_H__ >= 202311L +#define MP_CKD_MUL(result, a, b) ckd_mul(result, a, b) +#elif __has_builtin(__builtin_mul_overflow) +#define MP_CKD_MUL(result, a, b) __builtin_mul_overflow(a, b, result) +#endif + #endif