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 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/math.c (limited to 'src/math.c') 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; +} -- cgit v1.2.3