From eb14d0cc649978c10928bf38c1847f295bf0de69 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 4 Apr 2017 12:06:57 +0200 Subject: [PATCH] Add BSD-specific reallocarray() It is available in FreeBSD, NetBSD and OpenBSD, but not in glibc. It is used for example by OpenSSH. --- newlib/libc/include/stdlib.h | 2 ++ newlib/libc/stdlib/Makefile.am | 1 + newlib/libc/stdlib/Makefile.in | 40 ++++++++++++++++------------- newlib/libc/stdlib/reallocarray.c | 42 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 newlib/libc/stdlib/reallocarray.c diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h index 9ee32aced..968367f22 100644 --- a/newlib/libc/include/stdlib.h +++ b/newlib/libc/include/stdlib.h @@ -140,6 +140,8 @@ _VOID _EXFUN(qsort,(_PTR __base, size_t __nmemb, size_t __size, __compar_fn_t _c int _EXFUN(rand,(_VOID)); _PTR _EXFUN_NOTHROW(realloc,(_PTR __r, size_t __size)); #if __BSD_VISIBLE +void *reallocarray(void *, size_t, size_t) __result_use_check __alloc_size(2) + __alloc_size(3); _PTR _EXFUN(reallocf,(_PTR __r, size_t __size)); #endif #if __BSD_VISIBLE || __XSI_VISIBLE >= 4 diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index fb2e44e42..ebae00ab0 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -52,6 +52,7 @@ GENERAL_SOURCES = \ rand_r.c \ random.c \ realloc.c \ + reallocarray.c \ reallocf.c \ sb_charsets.c \ strtod.c \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index b4b70b42b..2cf297cb1 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -104,15 +104,15 @@ am__objects_2 = lib_a-__adjust.$(OBJEXT) lib_a-__atexit.$(OBJEXT) \ lib_a-mstats.$(OBJEXT) lib_a-on_exit_args.$(OBJEXT) \ lib_a-quick_exit.$(OBJEXT) lib_a-rand.$(OBJEXT) \ lib_a-rand_r.$(OBJEXT) lib_a-random.$(OBJEXT) \ - lib_a-realloc.$(OBJEXT) lib_a-reallocf.$(OBJEXT) \ - lib_a-sb_charsets.$(OBJEXT) lib_a-strtod.$(OBJEXT) \ - lib_a-strtodg.$(OBJEXT) lib_a-strtol.$(OBJEXT) \ - lib_a-strtorx.$(OBJEXT) lib_a-strtoul.$(OBJEXT) \ - lib_a-utoa.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \ - lib_a-wcstol.$(OBJEXT) lib_a-wcstoul.$(OBJEXT) \ - lib_a-wcstombs.$(OBJEXT) lib_a-wcstombs_r.$(OBJEXT) \ - lib_a-wctomb.$(OBJEXT) lib_a-wctomb_r.$(OBJEXT) \ - $(am__objects_1) + lib_a-realloc.$(OBJEXT) lib_a-reallocarray.$(OBJEXT) \ + lib_a-reallocf.$(OBJEXT) lib_a-sb_charsets.$(OBJEXT) \ + lib_a-strtod.$(OBJEXT) lib_a-strtodg.$(OBJEXT) \ + lib_a-strtol.$(OBJEXT) lib_a-strtorx.$(OBJEXT) \ + lib_a-strtoul.$(OBJEXT) lib_a-utoa.$(OBJEXT) \ + lib_a-wcstod.$(OBJEXT) lib_a-wcstol.$(OBJEXT) \ + lib_a-wcstoul.$(OBJEXT) lib_a-wcstombs.$(OBJEXT) \ + lib_a-wcstombs_r.$(OBJEXT) lib_a-wctomb.$(OBJEXT) \ + lib_a-wctomb_r.$(OBJEXT) $(am__objects_1) am__objects_3 = lib_a-arc4random.$(OBJEXT) \ lib_a-arc4random_uniform.$(OBJEXT) lib_a-cxa_atexit.$(OBJEXT) \ lib_a-cxa_finalize.$(OBJEXT) lib_a-drand48.$(OBJEXT) \ @@ -160,10 +160,10 @@ am__objects_9 = __adjust.lo __atexit.lo __call_atexit.lo __exp10.lo \ itoa.lo labs.lo ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo \ mbstowcs.lo mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo \ mprec.lo mstats.lo on_exit_args.lo quick_exit.lo rand.lo \ - rand_r.lo random.lo realloc.lo reallocf.lo sb_charsets.lo \ - strtod.lo strtodg.lo strtol.lo strtorx.lo strtoul.lo utoa.lo \ - wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo \ - wctomb.lo wctomb_r.lo $(am__objects_8) + rand_r.lo random.lo realloc.lo reallocarray.lo reallocf.lo \ + sb_charsets.lo strtod.lo strtodg.lo strtol.lo strtorx.lo \ + strtoul.lo utoa.lo wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo \ + wcstombs_r.lo wctomb.lo wctomb_r.lo $(am__objects_8) am__objects_10 = arc4random.lo arc4random_uniform.lo cxa_atexit.lo \ cxa_finalize.lo drand48.lo ecvtbuf.lo efgcvt.lo erand48.lo \ jrand48.lo lcong48.lo lrand48.lo mrand48.lo msize.lo mtrim.lo \ @@ -364,10 +364,10 @@ GENERAL_SOURCES = __adjust.c __atexit.c __call_atexit.c __exp10.c \ gdtoa-hexnan.c getenv.c getenv_r.c itoa.c labs.c ldiv.c \ ldtoa.c malloc.c mblen.c mblen_r.c mbstowcs.c mbstowcs_r.c \ mbtowc.c mbtowc_r.c mlock.c mprec.c mstats.c on_exit_args.c \ - quick_exit.c rand.c rand_r.c random.c realloc.c reallocf.c \ - sb_charsets.c strtod.c strtodg.c strtol.c strtorx.c strtoul.c \ - utoa.c wcstod.c wcstol.c wcstoul.c wcstombs.c wcstombs_r.c \ - wctomb.c wctomb_r.c $(am__append_1) + quick_exit.c rand.c rand_r.c random.c realloc.c reallocarray.c \ + reallocf.c sb_charsets.c strtod.c strtodg.c strtol.c strtorx.c \ + strtoul.c utoa.c wcstod.c wcstol.c wcstoul.c wcstombs.c \ + wcstombs_r.c wctomb.c wctomb_r.c $(am__append_1) @NEWLIB_NANO_MALLOC_FALSE@MALIGNR = malignr @NEWLIB_NANO_MALLOC_TRUE@MALIGNR = nano-malignr @NEWLIB_NANO_MALLOC_FALSE@MALLOPTR = malloptr @@ -900,6 +900,12 @@ lib_a-realloc.o: realloc.c lib_a-realloc.obj: realloc.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-realloc.obj `if test -f 'realloc.c'; then $(CYGPATH_W) 'realloc.c'; else $(CYGPATH_W) '$(srcdir)/realloc.c'; fi` +lib_a-reallocarray.o: reallocarray.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-reallocarray.o `test -f 'reallocarray.c' || echo '$(srcdir)/'`reallocarray.c + +lib_a-reallocarray.obj: reallocarray.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-reallocarray.obj `if test -f 'reallocarray.c'; then $(CYGPATH_W) 'reallocarray.c'; else $(CYGPATH_W) '$(srcdir)/reallocarray.c'; fi` + lib_a-reallocf.o: reallocf.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-reallocf.o `test -f 'reallocf.c' || echo '$(srcdir)/'`reallocf.c diff --git a/newlib/libc/stdlib/reallocarray.c b/newlib/libc/stdlib/reallocarray.c new file mode 100644 index 000000000..4b6cccb70 --- /dev/null +++ b/newlib/libc/stdlib/reallocarray.c @@ -0,0 +1,42 @@ +/* $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. + */ + +#include +__FBSDID("$FreeBSD: head/lib/libc/stdlib/reallocarray.c 282314 2015-05-01 18:32:16Z bapt $"); + +#include +#include +#include +#include + +/* + * 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 * +reallocarray(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)); +} -- 2.43.5