summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-08-06 15:38:58 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-08-06 16:22:02 -0700
commit621f61f7d3f5955a84e6aa8b7458699870fdee45 (patch)
tree5125afc313de6f2494940a47a8ae1d9cb4163b06
parent05ee465685aba409800523c6615392a867366818 (diff)
downloadxorg-lib-libXmu-621f61f7d3f5955a84e6aa8b7458699870fdee45.tar.gz
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>
-rw-r--r--COPYING18
-rw-r--r--configure.ac8
-rw-r--r--src/Makefile.am3
-rw-r--r--src/Xmuint.h42
-rw-r--r--src/reallocarray.c43
5 files changed, 113 insertions, 1 deletions
diff --git a/COPYING b/COPYING
index b0fbd3e..2ca55c2 100644
--- a/COPYING
+++ b/COPYING
@@ -73,3 +73,21 @@ Except as contained in this notice, the name of the XFree86 Project shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from the
XFree86 Project.
+
+-----------
+
+src/reallocarray.c has:
+
+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.
diff --git a/configure.ac b/configure.ac
index 50ac183..68f685f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6,6 +6,11 @@ AC_INIT([libXmu], [1.1.3],
AC_CONFIG_SRCDIR([Makefile.am])
AC_CONFIG_HEADERS([config.h])
+# Set common system defines for POSIX extensions, such as _GNU_SOURCE
+# Must be called before any macros that run the compiler (like AC_PROG_LIBTOOL)
+# to avoid autoconf errors.
+AC_USE_SYSTEM_EXTENSIONS
+
# Initialize Automake
AM_INIT_AUTOMAKE([foreign dist-xz])
@@ -31,6 +36,9 @@ PKG_CHECK_MODULES(XMUU, x11)
# conversion routines for
XTRANS_CONNECTION_FLAGS
+# Checks for library functions.
+AC_REPLACE_FUNCS([reallocarray])
+
# Allow checking code with lint, sparse, etc.
XORG_WITH_LINT
XORG_LINT_LIBRARY([Xmu])
diff --git a/src/Makefile.am b/src/Makefile.am
index 3aa88d1..a08a8bb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -12,7 +12,7 @@ AM_CFLAGS = $(CWARNFLAGS) $(XMU_CFLAGS)
libXmu_la_LDFLAGS = -version-number 6:2:0 -no-undefined
libXmuu_la_LDFLAGS = -version-number 1:0:0 -no-undefined
-libXmu_la_LIBADD = $(XMU_LIBS)
+libXmu_la_LIBADD = $(LTLIBOBJS) $(XMU_LIBS)
libXmuu_la_LIBADD = $(XMUU_LIBS)
libXmuu_la_SOURCES = \
@@ -24,6 +24,7 @@ libXmuu_la_SOURCES = \
libXmu_la_SOURCES = \
$(libXmuu_la_SOURCES) \
+ Xmuint.h \
AllCmap.c \
Atoms.c \
Clip.c \
diff --git a/src/Xmuint.h b/src/Xmuint.h
new file mode 100644
index 0000000..85bc235
--- /dev/null
+++ b/src/Xmuint.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022, Oracle and/or its affiliates.
+ *
+ * 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.
+ */
+
+#ifndef XMUINT_H
+#define XMUINT_H
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <X11/Xfuncproto.h>
+#include <sys/types.h>
+
+#ifndef HAVE_REALLOCARRAY
+extern _X_HIDDEN void *Xmureallocarray(void *optr, size_t nmemb, size_t size);
+# define reallocarray(ptr, n, size) Xmureallocarray((ptr), (n), (size))
+#endif
+
+#define Xmumallocarray(n, size) reallocarray(NULL, (n), (size))
+
+#endif /* XMUINT_H */
diff --git a/src/reallocarray.c b/src/reallocarray.c
new file mode 100644
index 0000000..ade402c
--- /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 "Xmuint.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 *
+Xmureallocarray(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);
+}