From fa2862fa648cca9a181184a849444409d07e9fb2 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 31 May 2024 11:48:58 -0300 Subject: src/math.h: Add "add_size()" --- src/math.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/math.c') diff --git a/src/math.c b/src/math.c index 366bd5e..4e9fee0 100644 --- a/src/math.c +++ b/src/math.c @@ -1,6 +1,7 @@ #include "config.h" #include +#include #include #include #include @@ -8,9 +9,24 @@ #include "math.h" +int +add_size(const size_t x, const size_t y, size_t *const out) { + int rc = EOVERFLOW; + + const bool overflows = (SIZE_MAX - x) < y; + if (overflows) { + goto out; + } + + *out = x + y; + rc = 0; +out: + return rc; +} + int mul_size(const size_t x, const size_t y, size_t *const out) { - int rc = -1; + int rc = EOVERFLOW; assert(x != 0U); assert(y != 0U); -- cgit v1.2.3