summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/math.c18
-rw-r--r--src/math.h3
2 files changed, 20 insertions, 1 deletions
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 <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);
diff --git a/src/math.h b/src/math.h
index 13f1795..4dab4a7 100644
--- a/src/math.h
+++ b/src/math.h
@@ -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);