commit ab82a9df2192a67fbb578a1845d1ddfa06dd4420
parent 1aab192eee09e5cbce1804091f6ddaa4ccbf75c9
Author: Frederic Cambus <fred@statdns.com>
Date: Mon, 28 Sep 2020 17:52:55 +0200
Add a compat layer for systems which do not have reallocarray(3).
Diffstat:
6 files changed, 53 insertions(+), 0 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -12,6 +12,7 @@ include(GNUInstallDirs)
# Check if system has strtonum
check_function_exists(strtonum HAVE_STRTONUM)
+check_function_exists(reallocarray HAVE_REALLOCARRAY)
# Additional include directories for compat functions and headers
include_directories("compat" "include" "src")
@@ -28,6 +29,10 @@ if(NOT HAVE_STRTONUM)
set (SRC ${SRC} compat/strtonum.c)
endif()
+if(NOT HAVE_REALLOCARRAY)
+ set (SRC ${SRC} compat/reallocarray.c)
+endif()
+
add_definitions(-D_GNU_SOURCE -Wall -Wextra -std=c99 -pedantic)
add_library(ansilove SHARED ${SRC} ${LOADERS})
diff --git a/THANKS b/THANKS
@@ -20,6 +20,7 @@ Fonts:
Code:
- Ted Unangst and Todd Miller for strtonum.c
+- Otto Moerbeek for reallocarray.c
diff --git a/compat/reallocarray.c b/compat/reallocarray.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/compat/reallocarray.h b/compat/reallocarray.h
@@ -0,0 +1 @@
+void *reallocarray(void *, size_t, size_t);
diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c
@@ -27,6 +27,10 @@
#include "strtonum.h"
#endif
+#ifndef HAVE_REALLOCARRAY
+#include "reallocarray.h"
+#endif
+
#define ANSI_SEQUENCE_MAX_LENGTH 14
#define ANSI_BUFFER_SIZE 65536
diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c
@@ -20,6 +20,10 @@
#include "fonts.h"
#include "output.h"
+#ifndef HAVE_REALLOCARRAY
+#include "reallocarray.h"
+#endif
+
#define STATE_TEXT 0
#define STATE_SEQUENCE 1
#define STATE_END 2