aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compat/reallocarray.c40
-rwxr-xr-xconfigure9
-rw-r--r--have/reallocarray.c7
3 files changed, 55 insertions, 1 deletions
diff --git a/compat/reallocarray.c b/compat/reallocarray.c
new file mode 100644
index 0000000..7c8c1df
--- /dev/null
+++ b/compat/reallocarray.c
@@ -0,0 +1,40 @@
+/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther 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 "../config.h"
+
+#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 ((size_t)1 << (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);
+}
diff --git a/configure b/configure
index cfec87a..84ed361 100755
--- a/configure
+++ b/configure
@@ -227,6 +227,7 @@ runtest openssl OPENSSL || true
runtest pr_set_name PR_SET_NAME || true
runtest program_invocation_short_name PROGRAM_INVOCATION_SHORT_NAME "" -D_GNU_SOURCE || true
runtest queue_h QUEUE_H || true
+runtest reallocarray REALLOCARRAY || true
runtest recallocarray RECALLOCARRAY || true
runtest setproctitle SETPROCTITLE || true
runtest strlcat STRLCAT || true
@@ -298,6 +299,7 @@ cat <<__HEREDOC__
#define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
#define HAVE_PR_SET_NAME ${HAVE_PR_SET_NAME}
#define HAVE_QUEUE_H ${HAVE_QUEUE_H}
+#define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
#define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
#define HAVE_SETPROCTITLE ${HAVE_SETPROCTITLE}
#define HAVE_STRLCAT ${HAVE_STRLCAT}
@@ -309,6 +311,7 @@ __HEREDOC__
[ ${HAVE_EXPLICIT_BZERO} -eq 0 -o \
${HAVE_FREEZERO} -eq 0 -o \
+ ${HAVE_REALLOCARRAY} -eq 0 -o \
${HAVE_RECALLOCARRAY} -eq 0 -o \
${HAVE_STRLCAT} -eq 0 -o \
${HAVE_STRLCPY} -eq 0 -o \
@@ -346,8 +349,12 @@ fi
if [ ${HAVE_IMSG} -eq 0 ]; then
COMPAT="${COMPAT} compat/imsg.o compat/imsg-buffer.o"
fi
+if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
+ echo "extern void *reallocarray(void*, size_t, size_t);"
+ COMPAT="${COMPAT} compat/reallocarray.o"
+fi
if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
- echo "extern void* recallocarray(void*, size_t, size_t, size_t);"
+ echo "extern void *recallocarray(void*, size_t, size_t, size_t);"
COMPAT="${COMPAT} compat/recallocarray.o"
fi
if [ ${HAVE_SETPROCTITLE} -eq 0 ]; then
diff --git a/have/reallocarray.c b/have/reallocarray.c
new file mode 100644
index 0000000..f99e685
--- /dev/null
+++ b/have/reallocarray.c
@@ -0,0 +1,7 @@
+#include <stdlib.h>
+
+int
+main(void)
+{
+ return !reallocarray(NULL, 2, 2);
+}