diff options
author | EuAndreh <eu@euandre.org> | 2024-05-31 11:48:58 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-05-31 11:48:58 -0300 |
commit | fa2862fa648cca9a181184a849444409d07e9fb2 (patch) | |
tree | 3f3e79879df960b493f9c5f903464b03dc321855 /src | |
parent | src/util.h: Add EXIT_USAGE (diff) | |
download | pindaiba-fa2862fa648cca9a181184a849444409d07e9fb2.tar.gz pindaiba-fa2862fa648cca9a181184a849444409d07e9fb2.tar.xz |
src/math.h: Add "add_size()"
Diffstat (limited to 'src')
-rw-r--r-- | src/math.c | 18 | ||||
-rw-r--r-- | src/math.h | 3 |
2 files changed, 20 insertions, 1 deletions
@@ -1,6 +1,7 @@ #include "config.h" #include <assert.h> +#include <errno.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -9,8 +10,23 @@ 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); @@ -1,2 +1,5 @@ int +add_size(const size_t x, const size_t y, size_t *const out); + +int mul_size(const size_t x, const size_t y, size_t *const out); |