2025-04-29 23:08:15 -04:00
|
|
|
/* Overflow-safe math helper macros
|
2022-02-27 14:47:31 +00:00
|
|
|
*
|
|
|
|
* To the extent possible under law, the authors have waived all
|
|
|
|
* copyright and related or neighboring rights to this code. For
|
|
|
|
* details, see the Creative Commons Zero 1.0 Universal license at
|
|
|
|
* https://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
*/
|
|
|
|
|
2025-04-29 23:08:15 -04:00
|
|
|
#pragma once
|
2022-02-27 14:47:31 +00:00
|
|
|
|
2023-06-18 19:23:58 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
#define ws_safe_op_jmp(op, res, a, b, env) \
|
|
|
|
do { \
|
2025-04-29 23:08:15 -04:00
|
|
|
if(ckd_##op(res, a, b)) { \
|
2023-06-18 19:23:58 +01:00
|
|
|
longjmp(env, 1); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ws_safe_add_jmp(res, a, b, env) ws_safe_op_jmp(add, res, a, b, env)
|
|
|
|
#define ws_safe_sub_jmp(res, a, b, env) ws_safe_op_jmp(sub, res, a, b, env)
|
|
|
|
#define ws_safe_mul_jmp(res, a, b, env) ws_safe_op_jmp(mul, res, a, b, env)
|