[Midnightbsd-cvs] src [7140] trunk: Bring in reallocarray(3) from OpenBSD CURRENT.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat Jul 25 15:03:21 EDT 2015


Revision: 7140
          http://svnweb.midnightbsd.org/src/?rev=7140
Author:   laffer1
Date:     2015-07-25 15:03:20 -0400 (Sat, 25 Jul 2015)
Log Message:
-----------
Bring in reallocarray(3) from OpenBSD CURRENT.

The reallocarray() function is similar to realloc() except it operates on nmemb members of size size and checks for integer overflow in the calculation nmemb * size.

Modified Paths:
--------------
    trunk/include/stdlib.h
    trunk/lib/libc/Versions.def
    trunk/lib/libc/stdlib/Makefile.inc
    trunk/lib/libc/stdlib/Symbol.map
    trunk/lib/libc/stdlib/malloc.3

Added Paths:
-----------
    trunk/lib/libc/stdlib/reallocarray.c

Modified: trunk/include/stdlib.h
===================================================================
--- trunk/include/stdlib.h	2015-07-25 04:23:17 UTC (rev 7139)
+++ trunk/include/stdlib.h	2015-07-25 19:03:20 UTC (rev 7140)
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)stdlib.h	8.5 (Berkeley) 5/19/95
- * $MidnightBSD: src/include/stdlib.h,v 1.6 2012/10/20 17:17:25 laffer1 Exp $
+ * $MidnightBSD$
  */
 
 #ifndef _STDLIB_H_
@@ -103,6 +103,9 @@
 	    int (*)(const void *, const void *));
 int	 rand(void);
 void	*realloc(void *, size_t);
+#if __BSD_VISIBLE
+void    *reallocarray(void *, size_t, size_t);
+#endif
 void	 srand(unsigned);
 double	 strtod(const char * __restrict, char ** __restrict);
 float	 strtof(const char * __restrict, char ** __restrict);

Modified: trunk/lib/libc/Versions.def
===================================================================
--- trunk/lib/libc/Versions.def	2015-07-25 04:23:17 UTC (rev 7139)
+++ trunk/lib/libc/Versions.def	2015-07-25 19:03:20 UTC (rev 7140)
@@ -23,9 +23,12 @@
 MNBSD_1.1 {
 } MNBSD_1.0;
 
+MNBSD_1.2 {
+} MNBSD_1.1;
+
 # This is our private namespace.  Any global interfaces that are
 # strictly for use only by other FreeBSD applications and libraries
 # are listed here.  We use a separate namespace so we can write
 # simple ABI-checking tools.
 FBSDprivate_1.0 {
-} MNBSD_1.1;
+} MNBSD_1.2;

Modified: trunk/lib/libc/stdlib/Makefile.inc
===================================================================
--- trunk/lib/libc/stdlib/Makefile.inc	2015-07-25 04:23:17 UTC (rev 7139)
+++ trunk/lib/libc/stdlib/Makefile.inc	2015-07-25 19:03:20 UTC (rev 7140)
@@ -9,7 +9,7 @@
 	getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c \
 	insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c malloc.c \
 	merge.c ptsname.c qsort.c qsort_r.c quick_exit.c radixsort.c rand.c \
-	random.c reallocf.c realpath.c remque.c strfmon.c strtoimax.c \
+	random.c reallocarray.c reallocf.c realpath.c remque.c strfmon.c strtoimax.c \
 	strtol.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \
         strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c
 
@@ -48,7 +48,7 @@
 MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
 MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
 MLINKS+=malloc.3 calloc.3 malloc.3 free.3 malloc.3 malloc.conf.5 \
-	malloc.3 realloc.3 malloc.3 reallocf.3 malloc.3 malloc_usable_size.3
+	malloc.3 realloc.3 malloc.3 reallocarray.3 malloc.3 reallocf.3 malloc.3 malloc_usable_size.3
 MLINKS+=tsearch.3 tdelete.3 tsearch.3 tfind.3 tsearch.3 twalk.3
 
 .if defined(MALLOC_PRODUCTION)

Modified: trunk/lib/libc/stdlib/Symbol.map
===================================================================
--- trunk/lib/libc/stdlib/Symbol.map	2015-07-25 04:23:17 UTC (rev 7139)
+++ trunk/lib/libc/stdlib/Symbol.map	2015-07-25 19:03:20 UTC (rev 7140)
@@ -116,6 +116,10 @@
 	clearenv;
 };
 
+MNBSD_1.2 {
+	reallocarray;
+};
+
 FBSDprivate_1.0 {
 	_malloc_thread_cleanup;
 	_malloc_prefork;

Modified: trunk/lib/libc/stdlib/malloc.3
===================================================================
--- trunk/lib/libc/stdlib/malloc.3	2015-07-25 04:23:17 UTC (rev 7139)
+++ trunk/lib/libc/stdlib/malloc.3	2015-07-25 19:03:20 UTC (rev 7140)
@@ -30,13 +30,13 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)malloc.3	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD$
+.\" $MidnightBSD$
 .\"
-.Dd January 31, 2010
+.Dd July 25, 2015
 .Dt MALLOC 3
 .Os
 .Sh NAME
-.Nm malloc , calloc , realloc , free , reallocf , malloc_usable_size
+.Nm malloc , calloc , reallocarray, realloc , free , reallocf , malloc_usable_size
 .Nd general purpose memory allocation functions
 .Sh LIBRARY
 .Lb libc
@@ -47,6 +47,8 @@
 .Ft void *
 .Fn calloc "size_t number" "size_t size"
 .Ft void *
+.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
+.Ft void *
 .Fn realloc "void *ptr" "size_t size"
 .Ft void *
 .Fn reallocf "void *ptr" "size_t size"
@@ -127,6 +129,13 @@
 causing memory leaks in libraries.
 .Pp
 The
+.Fn reallocarray
+function is similar to 
+.Fn realloc
+except it operates on nmemb members of size size and checks for integer overflow in
+the calculation nmemb * size.
+.Pp
+The
 .Fn free
 function causes the allocated memory referenced by
 .Fa ptr
@@ -589,3 +598,9 @@
 .Fn malloc_usable_size
 function first appeared in
 .Fx 7.0 .
+.Pp
+The
+.Fn reallocarray
+function first appeared in 
+OpenBSD 5.6 and was added to
+.Mx 0.7 .

Added: trunk/lib/libc/stdlib/reallocarray.c
===================================================================
--- trunk/lib/libc/stdlib/reallocarray.c	                        (rev 0)
+++ trunk/lib/libc/stdlib/reallocarray.c	2015-07-25 19:03:20 UTC (rev 7140)
@@ -0,0 +1,38 @@
+/*	$OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $	*/
+/*
+ * Copyright (c) 2008 Otto Moerbeek <otto at 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.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.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 *
+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);
+}


Property changes on: trunk/lib/libc/stdlib/reallocarray.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property


More information about the Midnightbsd-cvs mailing list