From fef0a487a119a5cbcd2fd95c6a35e0a9c4ab8ac1 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 24 May 2024 11:35:20 -0300 Subject: src/math.c: Add mul_size() --- src/math.c | 27 +++++++++++++++++++++++++++ src/math.h | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 src/math.c create mode 100644 src/math.h (limited to 'src') diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..366bd5e --- /dev/null +++ b/src/math.c @@ -0,0 +1,27 @@ +#include "config.h" + +#include +#include +#include +#include + +#include "math.h" + + +int +mul_size(const size_t x, const size_t y, size_t *const out) { + int rc = -1; + + assert(x != 0U); + assert(y != 0U); + + const bool overflows = x > (SIZE_MAX / y); + if (overflows) { + goto out; + } + + *out = x * y; + rc = 0; +out: + return rc; +} diff --git a/src/math.h b/src/math.h new file mode 100644 index 0000000..13f1795 --- /dev/null +++ b/src/math.h @@ -0,0 +1,2 @@ +int +mul_size(const size_t x, const size_t y, size_t *const out); -- cgit v1.2.3