From d965a1a8ce9331d2aaf1c697a29455ad55171b36 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 11 Jun 2022 10:00:29 -0700 Subject: Import reallocarray() from libX11 Originally from OpenBSD, based on libx11@bcf7b5aa Signed-off-by: Alan Coopersmith --- configure.ac | 1 + src/Makefile.am | 5 +++-- src/reallocarray.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/reallocarray.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/reallocarray.c create mode 100644 src/reallocarray.h diff --git a/configure.ac b/configure.ac index 86a479a..cffefcf 100644 --- a/configure.ac +++ b/configure.ac @@ -40,6 +40,7 @@ AC_SUBST(XEXT_SOREV) PKG_CHECK_MODULES(XEXT, [xproto >= 7.0.13] [x11 >= 1.6] [xextproto >= 7.1.99]) AX_GCC_BUILTIN([__builtin_popcountl]) +AC_REPLACE_FUNCS([reallocarray]) # Allow checking code with lint, sparse, etc. XORG_WITH_LINT diff --git a/src/Makefile.am b/src/Makefile.am index 2178f3e..5952b7f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,7 +9,7 @@ AM_CFLAGS=$(CWARNFLAGS) libXext_la_LDFLAGS = -version-number $(XEXT_SOREV) -no-undefined -libXext_la_LIBADD = $(XEXT_LIBS) +libXext_la_LIBADD = $(LTLIBOBJS) $(XEXT_LIBS) libXext_la_SOURCES = \ DPMS.c \ @@ -28,7 +28,8 @@ libXext_la_SOURCES = \ Xge.c \ extutil.c \ extutilP.h \ - globals.c + reallocarray.h \ + globals.c libXextincludedir = $(includedir)/X11/extensions libXextinclude_HEADERS = $(top_srcdir)/include/X11/extensions/dpms.h \ 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 + * + * 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 +#endif + +#include +#include +#include +#include +#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 +#endif + +#include +#include +#include + +#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)) -- cgit v1.2.1