diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 4 | ||||
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 21 | ||||
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 6 |
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 | |||
44 | MLINKS+=labs.3 llabs.3 | 44 | MLINKS+=labs.3 llabs.3 |
45 | MLINKS+=lsearch.3 lfind.3 | 45 | MLINKS+=lsearch.3 lfind.3 |
46 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 | 46 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 |
47 | MLINKS+=malloc.3 mallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5 | 47 | MLINKS+=malloc.3 reallocarray.3 malloc.3 cfree.3 malloc.3 malloc.conf.5 |
48 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | 48 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 |
49 | MLINKS+=radixsort.3 sradixsort.3 | 49 | MLINKS+=radixsort.3 sradixsort.3 |
50 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 | 50 | MLINKS+=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 |
97 | The multiplication may lead to an integer overflow, which can | 97 | The multiplication may lead to an integer overflow, which can |
98 | be avoided using the extension | 98 | be avoided using the extension |
99 | .Fn mallocarray , | 99 | .Fn reallocarray , |
100 | as follows: | 100 | as follows: |
101 | .Bd -literal -offset indent | 101 | .Bd -literal -offset indent |
102 | if ((p = mallocarray(num, size)) == NULL) | 102 | if ((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 . |
126 | The space is initialized to zero. | 126 | The space is initialized to zero. |
127 | The use of | 127 | The use of |
128 | .Fn reallocarray | ||
129 | or | ||
128 | .Fn calloc | 130 | .Fn calloc |
129 | is strongly encouraged when allocating multiple sized objects | 131 | is strongly encouraged when allocating multiple sized objects |
130 | in order to avoid possible integer overflows. | 132 | in order to avoid possible integer overflows. |
@@ -309,11 +311,6 @@ malloc_options = "X"; | |||
309 | .Pp | 311 | .Pp |
310 | Note that this will cause code that is supposed to handle | 312 | Note that this will cause code that is supposed to handle |
311 | out-of-memory conditions gracefully to abort instead. | 313 | out-of-memory conditions gracefully to abort instead. |
312 | .It Cm Z | ||
313 | .Dq Zero . | ||
314 | Fill some junk into the area allocated (see | ||
315 | .Cm J ) , | ||
316 | except 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 . |
319 | Decrease the size of the free page cache by a factor of two. | 316 | Decrease the size of the free page cache by a factor of two. |
@@ -494,6 +491,6 @@ random. | |||
494 | A rewrite by Otto Moerbeek introducing a new central data structure and more | 491 | A rewrite by Otto Moerbeek introducing a new central data structure and more |
495 | randomization appeared in | 492 | randomization appeared in |
496 | .Ox 4.4 . | 493 | .Ox 4.4 . |
497 | .Fn mallocarray | 494 | .Fn reallocarray |
498 | appeared in | 495 | appeared 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 | ||
1435 | void * | 1435 | void * |
1436 | mallocarray(size_t nmemb, size_t size) | 1436 | reallocarray(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 | ||
1446 | static void * | 1446 | static void * |