summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortedu <>2014-04-22 14:26:26 +0000
committertedu <>2014-04-22 14:26:26 +0000
commit5c557d38ed349266c22bfdf0d800f6e873921885 (patch)
treec8c9415e75220dc3ffecdcd042ee356dc6051ba3 /src
parent83c4dbaf581d987510fb450e4e68af50d52c6f99 (diff)
downloadopenbsd-5c557d38ed349266c22bfdf0d800f6e873921885.tar.gz
openbsd-5c557d38ed349266c22bfdf0d800f6e873921885.tar.bz2
openbsd-5c557d38ed349266c22bfdf0d800f6e873921885.zip
change mallocarray to reallocarray. useful in a few more situations.
malloc can, as always, be emulated via realloc(NULL). ok deraadt
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/Makefile.inc4
-rw-r--r--src/lib/libc/stdlib/malloc.321
-rw-r--r--src/lib/libc/stdlib/malloc.c6
3 files changed, 14 insertions, 17 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index c8dc01c4c4..83b42c34e3 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.51 2014/04/21 13:21:57 deraadt Exp $ 1# $OpenBSD: Makefile.inc,v 1.52 2014/04/22 14:26:26 tedu Exp $
2 2
3# stdlib sources 3# stdlib sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
@@ -44,7 +44,7 @@ MLINKS+=insque.3 remque.3
44MLINKS+=labs.3 llabs.3 44MLINKS+=labs.3 llabs.3
45MLINKS+=lsearch.3 lfind.3 45MLINKS+=lsearch.3 lfind.3
46MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 46MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3
47MLINKS+=malloc.3 mallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5 47MLINKS+=malloc.3 reallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5
48MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 48MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
49MLINKS+=radixsort.3 sradixsort.3 49MLINKS+=radixsort.3 sradixsort.3
50MLINKS+=rand.3 srand.3 rand.3 rand_r.3 50MLINKS+=rand.3 srand.3 rand.3 rand_r.3
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 2a636b88cc..2f99ea9b80 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,15 +30,15 @@
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE. 31.\" SUCH DAMAGE.
32.\" 32.\"
33.\" $OpenBSD: malloc.3,v 1.74 2014/04/21 13:17:32 deraadt Exp $ 33.\" $OpenBSD: malloc.3,v 1.75 2014/04/22 14:26:26 tedu Exp $
34.\" 34.\"
35.Dd $Mdocdate: April 21 2014 $ 35.Dd $Mdocdate: April 22 2014 $
36.Dt MALLOC 3 36.Dt MALLOC 3
37.Os 37.Os
38.Sh NAME 38.Sh NAME
39.Nm malloc , 39.Nm malloc ,
40.Nm calloc , 40.Nm calloc ,
41.Nm mallocarray , 41.Nm reallocarray ,
42.Nm realloc , 42.Nm realloc ,
43.Nm free , 43.Nm free ,
44.Nm cfree 44.Nm cfree
@@ -50,7 +50,7 @@
50.Ft void * 50.Ft void *
51.Fn calloc "size_t nmemb" "size_t size" 51.Fn calloc "size_t nmemb" "size_t size"
52.Ft void * 52.Ft void *
53.Fn mallocarray "size_t nmemb" "size_t size" 53.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
54.Ft void * 54.Ft void *
55.Fn realloc "void *ptr" "size_t size" 55.Fn realloc "void *ptr" "size_t size"
56.Ft void 56.Ft void
@@ -96,10 +96,10 @@ if ((p = malloc(num * size)) == NULL)
96.Pp 96.Pp
97The multiplication may lead to an integer overflow, which can 97The multiplication may lead to an integer overflow, which can
98be avoided using the extension 98be avoided using the extension
99.Fn mallocarray , 99.Fn reallocarray ,
100as follows: 100as follows:
101.Bd -literal -offset indent 101.Bd -literal -offset indent
102if ((p = mallocarray(num, size)) == NULL) 102if ((p = reallocarray(NULL, num, size)) == NULL)
103 err(1, "malloc"); 103 err(1, "malloc");
104.Ed 104.Ed
105.Pp 105.Pp
@@ -125,6 +125,8 @@ objects, each of whose size is
125.Fa size . 125.Fa size .
126The space is initialized to zero. 126The space is initialized to zero.
127The use of 127The use of
128.Fn reallocarray
129or
128.Fn calloc 130.Fn calloc
129is strongly encouraged when allocating multiple sized objects 131is strongly encouraged when allocating multiple sized objects
130in order to avoid possible integer overflows. 132in order to avoid possible integer overflows.
@@ -309,11 +311,6 @@ malloc_options = "X";
309.Pp 311.Pp
310Note that this will cause code that is supposed to handle 312Note that this will cause code that is supposed to handle
311out-of-memory conditions gracefully to abort instead. 313out-of-memory conditions gracefully to abort instead.
312.It Cm Z
313.Dq Zero .
314Fill some junk into the area allocated (see
315.Cm J ) ,
316except for the exact length the user asked for, which is zeroed.
317.It Cm < 314.It Cm <
318.Dq Half the cache size . 315.Dq Half the cache size .
319Decrease the size of the free page cache by a factor of two. 316Decrease the size of the free page cache by a factor of two.
@@ -494,6 +491,6 @@ random.
494A rewrite by Otto Moerbeek introducing a new central data structure and more 491A rewrite by Otto Moerbeek introducing a new central data structure and more
495randomization appeared in 492randomization appeared in
496.Ox 4.4 . 493.Ox 4.4 .
497.Fn mallocarray 494.Fn reallocarray
498appeared in 495appeared in
499.Ox 5.6 . 496.Ox 5.6 .
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index b48dcb3978..5790781733 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.154 2014/04/21 13:17:32 deraadt Exp $ */ 1/* $OpenBSD: malloc.c,v 1.155 2014/04/22 14:26:26 tedu Exp $ */
2/* 2/*
3 * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> 4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -1433,14 +1433,14 @@ calloc(size_t nmemb, size_t size)
1433} 1433}
1434 1434
1435void * 1435void *
1436mallocarray(size_t nmemb, size_t size) 1436reallocarray(void *optr, size_t nmemb, size_t size)
1437{ 1437{
1438 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 1438 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1439 nmemb > 0 && SIZE_MAX / nmemb < size) { 1439 nmemb > 0 && SIZE_MAX / nmemb < size) {
1440 errno = ENOMEM; 1440 errno = ENOMEM;
1441 return NULL; 1441 return NULL;
1442 } 1442 }
1443 return malloc(size * nmemb); 1443 return realloc(optr, size * nmemb);
1444} 1444}
1445 1445
1446static void * 1446static void *