From 3ff95c472abe8eb8fe1ccda5cd40d99407f1f9a4 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 8 May 2022 11:14:11 -0700 Subject: 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 --- configure.ac | 3 +++ src/Makefile.am | 5 +++-- src/reallocarray.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/reallocarray.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/reallocarray.c create mode 100644 src/reallocarray.h diff --git a/configure.ac b/configure.ac index a8f8c40..7735f51 100644 --- a/configure.ac +++ b/configure.ac @@ -53,6 +53,9 @@ AC_CHECK_LIB(z, gzclose, [], # Obtain compiler/linker options for dependencies PKG_CHECK_MODULES(FONTENC, xproto) +# Checks for library functions. +AC_REPLACE_FUNCS([reallocarray]) + # Allow checking code with lint, sparse, etc. XORG_WITH_LINT LINT_FLAGS="${LINT_FLAGS} ${FONTENC_CFLAGS}" 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 + * + * 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.3