aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqfs/alloc.c')
-rw-r--r--lib/sqfs/alloc.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/sqfs/alloc.c b/lib/sqfs/alloc.c
new file mode 100644
index 0000000..e8305d8
--- /dev/null
+++ b/lib/sqfs/alloc.c
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * alloc.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "util.h"
+
+#include <stdlib.h>
+#include <errno.h>
+
+void *alloc_flex(size_t base_size, size_t item_size, size_t nmemb)
+{
+ size_t size;
+
+ if (SZ_MUL_OV(nmemb, item_size, &size) ||
+ SZ_ADD_OV(base_size, size, &size)) {
+ errno = EOVERFLOW;
+ return NULL;
+ }
+
+ return calloc(1, size);
+}
+
+void *alloc_array(size_t item_size, size_t nmemb)
+{
+ size_t size;
+
+ if (SZ_MUL_OV(nmemb, item_size, &size)) {
+ errno = EOVERFLOW;
+ return NULL;
+ }
+
+ return calloc(1, size);
+}