summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-05-08 11:14:11 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-05-08 11:15:19 -0700
commit3ff95c472abe8eb8fe1ccda5cd40d99407f1f9a4 (patch)
treed0de103c0810bc6a3b457f0491cc0cb5b3354740 /src
parenta6a3bd1365798783f3fa5552d4ab061d5636d15a (diff)
Import reallocarray() from libX11 (originally from OpenBSD)
Wrapper for realloc() that checks for overflow when multiplying arguments together, so we don't have to add overflow checks to every single call. For documentation on usage, see: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/calloc.3 Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am5
-rw-r--r--src/reallocarray.c43
-rw-r--r--src/reallocarray.h44
3 files changed, 90 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index e37043b..b2df9a6 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,7 +3,8 @@ lib_LTLIBRARIES = libfontenc.la
libfontenc_la_SOURCES = \
encparse.c \
fontenc.c \
- fontencI.h
+ fontencI.h \
+ reallocarray.h
AM_CFLAGS = \
$(FONTENC_CFLAGS) \
@@ -15,7 +16,7 @@ FONTENCDEFS = -DFONT_ENCODINGS_DIRECTORY=\"$(FONTENCDIR)/encodings.dir\"
AM_CPPFLAGS = -I$(top_srcdir)/include $(FONTENCDEFS)
-libfontenc_la_LIBADD = @FONTENC_LIBS@
+libfontenc_la_LIBADD = $(LTLIBOBJS) @FONTENC_LIBS@
libfontenc_la_LDFLAGS = -version-number 1:0:0 -no-undefined
diff --git a/src/reallocarray.c b/src/reallocarray.c
new file mode 100644
index 0000000..2c301bc
--- /dev/null
+++ b/src/reallocarray.c
@@ -0,0 +1,43 @@
+/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "reallocarray.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 *
+xreallocarray(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/src/reallocarray.h b/src/reallocarray.h
new file mode 100644
index 0000000..ee38ebf
--- /dev/null
+++ b/src/reallocarray.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <X11/Xfuncproto.h>
+
+#ifndef HAVE_REALLOCARRAY
+extern _X_HIDDEN void *xreallocarray(void *optr, size_t nmemb, size_t size);
+# define reallocarray(ptr, n, size) xreallocarray((ptr), (size_t)(n), (size_t)(size))
+#endif
+
+#if defined(MALLOC_0_RETURNS_NULL) || defined(__clang_analyzer__)
+# define Xreallocarray(ptr, n, size) \
+ reallocarray((ptr), ((n) == 0 ? 1 : (n)), size)
+#else
+# define Xreallocarray(ptr, n, size) reallocarray((ptr), (n), (size))
+#endif
+
+#define Xmallocarray(n, size) Xreallocarray(NULL, (n), (size))