]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Add reallocarray() function from OpenBSD
authorBenjamin Baier <programmer@netzbasis.de>
Fri, 27 Jun 2014 00:05:11 +0000 (02:05 +0200)
committerGuillem Jover <guillem@hadrons.org>
Thu, 17 Jul 2014 03:13:05 +0000 (05:13 +0200)
Signed-off-by: Guillem Jover <guillem@hadrons.org>
COPYING
include/bsd/stdlib.h
src/Makefile.am
src/libbsd.map
src/reallocarray.c [new file with mode: 0644]

diff --git a/COPYING b/COPYING
index 960c920f89047136b46818f168a5f424d7769f8b..51a1b278dd45b61d2e22a95d173160bf51460294 100644 (file)
--- 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.
index eb1f1c169e4a6e593b3c0f1dfc998a7695e1e958..22a6dc871bed39b2fa8d6490ffa9dd96216c19b7 100644 (file)
@@ -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);
index 950e47b2f04d38d9586edfa90373dadbc6008c64..b9ff0bc84ba9d31deb49f5cc16fb0fe5f775bd64 100644 (file)
@@ -66,6 +66,7 @@ libbsd_la_SOURCES = \
        progname.c \
        radixsort.c \
        readpassphrase.c \
+       reallocarray.c \
        reallocf.c \
        setmode.c \
        setproctitle.c \
index 35eb374f38e66b664f3f49e4636b3366f6a331a2..29e84fd5d8ea995a2547d486c2aac8a7f54abb1a 100644 (file)
@@ -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 (file)
index 0000000..7accd99
--- /dev/null
@@ -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);
+}