summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTed Unangst <tedu@cvs.openbsd.org>2014-04-22 14:26:27 +0000
committerTed Unangst <tedu@cvs.openbsd.org>2014-04-22 14:26:27 +0000
commit1e7c1a49a6919c7e8b5860f39946cafc2352bf8d (patch)
treefd36aaa467d9bc57a55c15861b14b1d8ee71d658
parentc0c1d1856bb3b7b90939e7bf1ed559a3a4f0a0fe (diff)
change mallocarray to reallocarray. useful in a few more situations.
malloc can, as always, be emulated via realloc(NULL). ok deraadt
-rw-r--r--include/stdlib.h4
-rw-r--r--lib/libc/stdlib/Makefile.inc4
-rw-r--r--lib/libc/stdlib/malloc.321
-rw-r--r--lib/libc/stdlib/malloc.c6
4 files changed, 16 insertions, 19 deletions
diff --git a/include/stdlib.h b/include/stdlib.h
index 93eddce71ab..71bb9f4fc87 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: stdlib.h,v 1.57 2014/04/21 13:17:32 deraadt Exp $ */
+/* $OpenBSD: stdlib.h,v 1.58 2014/04/22 14:26:26 tedu Exp $ */
/* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */
/*-
@@ -125,7 +125,7 @@ long labs(long);
ldiv_t ldiv(long, long);
void *malloc(size_t);
#if __BSD_VISIBLE
-void *mallocarray(size_t, size_t);
+void *reallocarray(void *, size_t, size_t);
#endif /* __BSD_VISIBLE */
void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
int rand(void);
diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc
index c8dc01c4c47..83b42c34e3d 100644
--- a/lib/libc/stdlib/Makefile.inc
+++ b/lib/libc/stdlib/Makefile.inc
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile.inc,v 1.51 2014/04/21 13:21:57 deraadt Exp $
+# $OpenBSD: Makefile.inc,v 1.52 2014/04/22 14:26:26 tedu Exp $
# stdlib sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
@@ -44,7 +44,7 @@ MLINKS+=insque.3 remque.3
MLINKS+=labs.3 llabs.3
MLINKS+=lsearch.3 lfind.3
MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3
-MLINKS+=malloc.3 mallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5
+MLINKS+=malloc.3 reallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5
MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
MLINKS+=radixsort.3 sradixsort.3
MLINKS+=rand.3 srand.3 rand.3 rand_r.3
diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3
index 2a636b88cc9..2f99ea9b80b 100644
--- a/lib/libc/stdlib/malloc.3
+++ b/lib/libc/stdlib/malloc.3
@@ -30,15 +30,15 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.\" $OpenBSD: malloc.3,v 1.74 2014/04/21 13:17:32 deraadt Exp $
+.\" $OpenBSD: malloc.3,v 1.75 2014/04/22 14:26:26 tedu Exp $
.\"
-.Dd $Mdocdate: April 21 2014 $
+.Dd $Mdocdate: April 22 2014 $
.Dt MALLOC 3
.Os
.Sh NAME
.Nm malloc ,
.Nm calloc ,
-.Nm mallocarray ,
+.Nm reallocarray ,
.Nm realloc ,
.Nm free ,
.Nm cfree
@@ -50,7 +50,7 @@
.Ft void *
.Fn calloc "size_t nmemb" "size_t size"
.Ft void *
-.Fn mallocarray "size_t nmemb" "size_t size"
+.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
.Ft void *
.Fn realloc "void *ptr" "size_t size"
.Ft void
@@ -96,10 +96,10 @@ if ((p = malloc(num * size)) == NULL)
.Pp
The multiplication may lead to an integer overflow, which can
be avoided using the extension
-.Fn mallocarray ,
+.Fn reallocarray ,
as follows:
.Bd -literal -offset indent
-if ((p = mallocarray(num, size)) == NULL)
+if ((p = reallocarray(NULL, num, size)) == NULL)
err(1, "malloc");
.Ed
.Pp
@@ -125,6 +125,8 @@ objects, each of whose size is
.Fa size .
The space is initialized to zero.
The use of
+.Fn reallocarray
+or
.Fn calloc
is strongly encouraged when allocating multiple sized objects
in order to avoid possible integer overflows.
@@ -309,11 +311,6 @@ malloc_options = "X";
.Pp
Note that this will cause code that is supposed to handle
out-of-memory conditions gracefully to abort instead.
-.It Cm Z
-.Dq Zero .
-Fill some junk into the area allocated (see
-.Cm J ) ,
-except for the exact length the user asked for, which is zeroed.
.It Cm <
.Dq Half the cache size .
Decrease the size of the free page cache by a factor of two.
@@ -494,6 +491,6 @@ random.
A rewrite by Otto Moerbeek introducing a new central data structure and more
randomization appeared in
.Ox 4.4 .
-.Fn mallocarray
+.Fn reallocarray
appeared in
.Ox 5.6 .
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c
index b48dcb39789..5790781733f 100644
--- a/lib/libc/stdlib/malloc.c
+++ b/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: malloc.c,v 1.154 2014/04/21 13:17:32 deraadt Exp $ */
+/* $OpenBSD: malloc.c,v 1.155 2014/04/22 14:26:26 tedu Exp $ */
/*
* Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net>
* Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -1433,14 +1433,14 @@ calloc(size_t nmemb, size_t size)
}
void *
-mallocarray(size_t nmemb, size_t size)
+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 malloc(size * nmemb);
+ return realloc(optr, size * nmemb);
}
static void *