commit a114b2a66fdb53c5b11213e3d49cf1faa52e784d
parent af50be4d25c2e20eb12440aea13f222545a9793e
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 5 Nov 2017 16:42:00 +0100
add util.c: reallocarray for musl
Diffstat:
4 files changed, 47 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,7 +6,7 @@ MANPREFIX = $(PREFIX)/man
CPPFLAGS = -I/usr/local/include
CFLAGS = -Wall -O3
LDFLAGS = -L/usr/local/lib -lpng
-OBJ = colors.o ff.o png.o
+OBJ = colors.o ff.o png.o util.o
BIN = colors
all: $(BIN)
@@ -14,9 +14,10 @@ all: $(BIN)
$(BIN): $(OBJ)
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $(OBJ) $(LDFLAGS)
-colors.o: arg.h colors.h tree.h
-ff.o: colors.h
-png.o: colors.h
+colors.o: arg.h colors.h tree.h util.h
+ff.o: colors.h util.h
+png.o: colors.h util.h
+util.o: util.h
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
diff --git a/ff.c b/ff.c
@@ -9,6 +9,7 @@
#include <string.h>
#include "colors.h"
+#include "util.h"
void
parseimg_ff(FILE *fp, void (*fn)(int, int, int))
diff --git a/util.c b/util.c
@@ -0,0 +1,38 @@
+/* $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 <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/util.h b/util.h
@@ -0,0 +1,3 @@
+/* See LICENSE file for copyright and license details. */
+#undef reallocarray
+void *reallocarray(void *, size_t, size_t);