summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2015-06-04 23:18:23 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2018-12-08 10:04:13 -0800
commitbcf7b5aa06c23aee00af7999b58cb96a8571ac42 (patch)
tree9edf012cc35ce19c8ff6bb4189f85e827c55d937
parent9bdfe9c9af11d77d66e5ff651e82b20e695cb460 (diff)
Import reallocarray() 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--configure.ac1
-rw-r--r--src/Makefile.am1
-rw-r--r--src/reallocarray.c43
-rw-r--r--src/reallocarray.h44
4 files changed, 89 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 30908453..5a3c7ce9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -145,6 +145,7 @@ AC_CHECK_HEADERS([sys/select.h])
# Checks for library functions.
AC_CHECK_FUNCS([strtol seteuid])
+AC_REPLACE_FUNCS([reallocarray])
# Used in lcFile.c (see also --enable-xlocaledir settings below)
XLOCALEDIR_IS_SAFE="no"
AC_CHECK_DECL([issetugid])
diff --git a/src/Makefile.am b/src/Makefile.am
index f8c476de..352d7ad0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -366,6 +366,7 @@ endif
libX11_la_LDFLAGS = -version-number 6:3:0 -no-undefined
libX11_la_LIBADD = \
+ $(LTLIBOBJS) \
$(USE_I18N_LIBS) \
$(USE_XCMS_LIBS) \
$(USE_XKB_LIBS) \
diff --git a/src/reallocarray.c b/src/reallocarray.c
new file mode 100644
index 00000000..2c301bc8
--- /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 00000000..32f535b6
--- /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), (n), (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))