Bring in reallocarray() from OpenBSD
6 files changed, 48 insertions(+), 2 deletions(-) | |||
---|---|---|---|
M | Makefile | +1 | -0 |
M | compat.h | +2 | -0 |
M | config.mk | +2 | -2 |
A | reallocarray.c | +39 | -0 |
M | strlcat.c | +2 | -0 |
M | strlcpy.c | +2 | -0 |
1@@ -6,6 +6,7 @@ SRC = \
2 stagit.c\
3 stagit-index.c
4 COMPATSRC = \
5+ reallocarray.c\
6 strlcat.c\
7 strlcpy.c
8 BIN = \
M · compat.h
+2, -01@@ -3,4 +3,6 @@
2 size_t strlcat(char *, const char *, size_t);
3 #undef strlcpy
4 size_t strlcpy(char *, const char *, size_t);
5+#undef reallocarray
6+void *reallocarray(void *, size_t, size_t);
7 #endif
M · config.mk
+2, -2 1@@ -28,8 +28,8 @@ LDFLAGS = ${LIBS}
2
3 # uncomment for compat
4 CFLAGS += -DCOMPAT
5-# uncomment if your libc doesn't support strlcat, strlcpy.
6-COMPATOBJ = strlcat.o strlcpy.o
7+# uncomment if your libc doesn't support reallocarray, strlcat, strlcpy.
8+COMPATOBJ = reallocarray.o strlcat.o strlcpy.o
9
10 # compiler and linker
11 #CC = cc
A · reallocarray.c
+39, -0 1@@ -0,0 +1,39 @@
2+/*
3+ * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4+ *
5+ * Permission to use, copy, modify, and distribute this software for any
6+ * purpose with or without fee is hereby granted, provided that the above
7+ * copyright notice and this permission notice appear in all copies.
8+ *
9+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16+ */
17+
18+#include <sys/types.h>
19+#include <errno.h>
20+#include <stdint.h>
21+#include <stdlib.h>
22+
23+#include "compat.h"
24+
25+/*
26+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
27+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
28+ */
29+#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
30+
31+void *
32+reallocarray(void *optr, size_t nmemb, size_t size)
33+{
34+ if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
35+ nmemb > 0 && SIZE_MAX / nmemb < size) {
36+ errno = ENOMEM;
37+ return NULL;
38+ }
39+ return realloc(optr, size * nmemb);
40+}
M · strlcat.c
+2, -01@@ -19,6 +19,8 @@
2 #include <sys/types.h>
3 #include <string.h>
4
5+#include "compat.h"
6+
7 /*
8 * Appends src to string dst of size dsize (unlike strncat, dsize is the
9 * full size of dst, not space left). At most dsize-1 characters
M · strlcpy.c
+2, -01@@ -19,6 +19,8 @@
2 #include <sys/types.h>
3 #include <string.h>
4
5+#include "compat.h"
6+
7 /*
8 * Copy string src to buffer dst of size dsize. At most dsize-1
9 * chars will be copied. Always NUL terminates (unless dsize == 0).