Add reallocarray() function from OpenBSD Signed-off-by: Guillem Jover <guillem@hadrons.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
diff --git a/COPYING b/COPYING
index 960c920..51a1b27 100644
--- a/COPYING
+++ b/COPYING
@@ -358,6 +358,8 @@ The rest of the licenses apply to code and/or man pages.
Copyright © 2004 Ted Unangst and Todd Miller
All rights reserved.
+ Copyright © 2008 Otto Moerbeek <otto@drijf.net>
+
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index eb1f1c1..22a6dc8 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -67,6 +67,7 @@ int sradixsort(const unsigned char **base, int nmemb,
const unsigned char *table, unsigned endbyte);
void *reallocf(void *ptr, size_t size);
+void *reallocarray(void *ptr, size_t nmemb, size_t size);
long long strtonum(const char *nptr, long long minval, long long maxval,
const char **errstr);
diff --git a/src/Makefile.am b/src/Makefile.am
index 950e47b..b9ff0bc 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -66,6 +66,7 @@ libbsd_la_SOURCES = \
progname.c \
radixsort.c \
readpassphrase.c \
+ reallocarray.c \
reallocf.c \
setmode.c \
setproctitle.c \
diff --git a/src/libbsd.map b/src/libbsd.map
index 35eb374..29e84fd 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -116,6 +116,8 @@ LIBBSD_0.7 {
funopen;
+ reallocarray;
+
sl_init;
sl_add;
sl_free;
diff --git a/src/reallocarray.c b/src/reallocarray.c
new file mode 100644
index 0000000..7accd99
--- /dev/null
+++ b/src/reallocarray.c
@@ -0,0 +1,38 @@
+/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */
+/*
+ * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
+
+void *
+reallocarray(void *optr, size_t nmemb, size_t size)
+{
+ if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+ nmemb > 0 && SIZE_MAX / nmemb < size) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ return realloc(optr, size * nmemb);
+}