From 77e54b25ee67ff066d8619d790b43cb2da227ae6 Mon Sep 17 00:00:00 2001 From: Michael McConville Date: Fri, 11 Dec 2015 23:04:36 -0500 Subject: Added new function reallocarray. This is taken from OpenSSH Portable, which in turn takes it from OpenBSD. reallocarray wraps the stdlib's realloc function. It takes two size arguments and checks for overflow, like calloc, but doesn't zero the memory. Therefore, it allows us to do overflow-safe array reallocations and overflow-safe unzeroed array allocations, which the stdlib allocation functions don't. We have a bunch of specific array allocation macros, none of which check for overflow. reallocarray should be able to replace them. --- lib/reallocarray.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/reallocarray.c (limited to 'lib') diff --git a/lib/reallocarray.c b/lib/reallocarray.c new file mode 100644 index 0000000..11d1e7b --- /dev/null +++ b/lib/reallocarray.c @@ -0,0 +1,46 @@ +/* $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. + */ + +/* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */ + +#include +#ifndef HAVE_REALLOCARRAY + +#include +#include +#ifdef HAVE_STDINT_H +#include +#endif +#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); +} +#endif /* HAVE_REALLOCARRAY */ -- cgit v1.2.3 From 64f27743785e30855b486e4575429192fddaa9eb Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 16 Feb 2016 14:35:34 +0100 Subject: Converted K&R style function definitions to ANSI C style Consistently make use of the ANSI C function definition style instead of the K&R style. --- lib/lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index a8ff70f..e540e8d 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -3,5 +3,5 @@ * moving something that flex treats as a library function into this * directory. */ -void do_nothing(){ return;} +void do_nothing(void){ return;} -- cgit v1.2.3 From 9132612c2c6724f716ca3f831a22e328b40a96f6 Mon Sep 17 00:00:00 2001 From: rlar Date: Sat, 27 Feb 2016 17:27:11 +0100 Subject: warning: no previous prototype for 'do_nothing' [-Wmissing-prototypes] --- lib/lib.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index e540e8d..4b4bf73 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -3,5 +3,7 @@ * moving something that flex treats as a library function into this * directory. */ +extern void do_nothing(void); + void do_nothing(void){ return;} -- cgit v1.2.3 From 80484ea2673393bf725ade8e72cda98983fa85cb Mon Sep 17 00:00:00 2001 From: Will Estes Date: Sun, 28 Feb 2016 16:23:19 -0500 Subject: Prototyped reallocarray implementation --- lib/reallocarray.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib') diff --git a/lib/reallocarray.c b/lib/reallocarray.c index 11d1e7b..0c1e250 100644 --- a/lib/reallocarray.c +++ b/lib/reallocarray.c @@ -19,6 +19,7 @@ #include #ifndef HAVE_REALLOCARRAY +#undef reallocarray #include #include @@ -27,6 +28,8 @@ #endif #include +void *reallocarray(void *, size_t, size_t); + /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW -- cgit v1.2.3 From 487177cbb85bc4e88e468c71b27569054b8df090 Mon Sep 17 00:00:00 2001 From: Manoj Srivastava Date: Fri, 29 Jul 2016 17:19:37 -0700 Subject: Imported Upstream version 2.6.1 --- lib/Makefile.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/Makefile.in b/lib/Makefile.in index 158a047..30a2fe8 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -174,7 +174,8 @@ am__define_uniq_tagged_files = \ ETAGS = etags CTAGS = ctags am__DIST_COMMON = $(srcdir)/Makefile.in \ - $(top_srcdir)/build-aux/depcomp malloc.c realloc.c + $(top_srcdir)/build-aux/depcomp malloc.c realloc.c \ + reallocarray.c DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) ACLOCAL = @ACLOCAL@ ALLOCA = @ALLOCA@ @@ -264,6 +265,7 @@ SET_MAKE = @SET_MAKE@ SHARED_VERSION_INFO = @SHARED_VERSION_INFO@ SHELL = @SHELL@ STRIP = @STRIP@ +TEXI2DVI = @TEXI2DVI@ USE_NLS = @USE_NLS@ VERSION = @VERSION@ XGETTEXT = @XGETTEXT@ @@ -383,6 +385,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/malloc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/realloc.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/reallocarray.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lib.Plo@am__quote@ .c.o: -- cgit v1.2.3