summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/math.c27
-rw-r--r--src/math.h2
2 files changed, 29 insertions, 0 deletions
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 <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#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);