diff options
author | deraadt <> | 2014-04-21 13:17:32 +0000 |
---|---|---|
committer | deraadt <> | 2014-04-21 13:17:32 +0000 |
commit | b5701aa73a632ec384dd8e14c19bf595b62d37c7 (patch) | |
tree | 2e743eaa4b5a39473daad0b655461bf389ae5a12 /src | |
parent | c7cbcf9bb8536d33ffb30f6eb6a5f8a4776ede31 (diff) | |
download | openbsd-b5701aa73a632ec384dd8e14c19bf595b62d37c7.tar.gz openbsd-b5701aa73a632ec384dd8e14c19bf595b62d37c7.tar.bz2 openbsd-b5701aa73a632ec384dd8e14c19bf595b62d37c7.zip |
Introducing: void *mallocarray(size_t nmemb, size_t size);
Like calloc(), except without the cleared-memory gaurantee
ok beck guenther, discussed for more than a year...
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 4 | ||||
-rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 29 | ||||
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 13 |
3 files changed, 36 insertions, 10 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 953c53382f..ec43b631b6 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.49 2014/03/18 22:36:29 miod Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.50 2014/04/21 13:17:32 deraadt 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 cfree.3 malloc.3 malloc.conf.5 | 47 | MLINKS+=malloc.3 mallocarray.5 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 414f0a9770..2a636b88cc 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -30,14 +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.73 2013/07/18 10:14:49 schwarze Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.74 2014/04/21 13:17:32 deraadt Exp $ |
34 | .\" | 34 | .\" |
35 | .Dd $Mdocdate: July 18 2013 $ | 35 | .Dd $Mdocdate: April 21 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 realloc , | 42 | .Nm realloc , |
42 | .Nm free , | 43 | .Nm free , |
43 | .Nm cfree | 44 | .Nm cfree |
@@ -49,12 +50,14 @@ | |||
49 | .Ft void * | 50 | .Ft void * |
50 | .Fn calloc "size_t nmemb" "size_t size" | 51 | .Fn calloc "size_t nmemb" "size_t size" |
51 | .Ft void * | 52 | .Ft void * |
53 | .Fn mallocarray "size_t nmemb" "size_t size" | ||
54 | .Ft void * | ||
52 | .Fn realloc "void *ptr" "size_t size" | 55 | .Fn realloc "void *ptr" "size_t size" |
53 | .Ft void | 56 | .Ft void |
54 | .Fn free "void *ptr" | 57 | .Fn free "void *ptr" |
55 | .Ft void | 58 | .Ft void |
56 | .Fn cfree "void *ptr" | 59 | .Fn cfree "void *ptr" |
57 | .Ft char * | 60 | .Ft char * Ns |
58 | .Va malloc_options ; | 61 | .Va malloc_options ; |
59 | .Sh DESCRIPTION | 62 | .Sh DESCRIPTION |
60 | The | 63 | The |
@@ -91,10 +94,18 @@ if ((p = malloc(num * size)) == NULL) | |||
91 | err(1, "malloc"); | 94 | err(1, "malloc"); |
92 | .Ed | 95 | .Ed |
93 | .Pp | 96 | .Pp |
94 | The multiplication may lead to an integer overflow. | 97 | The multiplication may lead to an integer overflow, which can |
95 | To avoid this, | 98 | be avoided using the extension |
99 | .Fn mallocarray , | ||
100 | as follows: | ||
101 | .Bd -literal -offset indent | ||
102 | if ((p = mallocarray(num, size)) == NULL) | ||
103 | err(1, "malloc"); | ||
104 | .Ed | ||
105 | .Pp | ||
106 | Alternatively | ||
96 | .Fn calloc | 107 | .Fn calloc |
97 | is recommended. | 108 | is a more portable solution which comes with the cost of clearing memory. |
98 | .Pp | 109 | .Pp |
99 | If | 110 | If |
100 | .Fn malloc | 111 | .Fn malloc |
@@ -324,7 +335,8 @@ it is buggy. | |||
324 | The default number of free pages cached is 64. | 335 | The default number of free pages cached is 64. |
325 | .Sh RETURN VALUES | 336 | .Sh RETURN VALUES |
326 | The | 337 | The |
327 | .Fn malloc | 338 | .Fn malloc , |
339 | .Fn mallocarray , | ||
328 | and | 340 | and |
329 | .Fn calloc | 341 | .Fn calloc |
330 | functions return a pointer to the allocated space if successful; otherwise, | 342 | functions return a pointer to the allocated space if successful; otherwise, |
@@ -482,3 +494,6 @@ random. | |||
482 | A rewrite by Otto Moerbeek introducing a new central data structure and more | 494 | A rewrite by Otto Moerbeek introducing a new central data structure and more |
483 | randomization appeared in | 495 | randomization appeared in |
484 | .Ox 4.4 . | 496 | .Ox 4.4 . |
497 | .Fn mallocarray | ||
498 | appeared in | ||
499 | .Ox 5.6 . | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 2cd443191d..b48dcb3978 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.153 2014/04/14 10:29:41 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.154 2014/04/21 13:17:32 deraadt 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> |
@@ -1432,6 +1432,17 @@ calloc(size_t nmemb, size_t size) | |||
1432 | return r; | 1432 | return r; |
1433 | } | 1433 | } |
1434 | 1434 | ||
1435 | void * | ||
1436 | mallocarray(size_t nmemb, size_t size) | ||
1437 | { | ||
1438 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && | ||
1439 | nmemb > 0 && SIZE_MAX / nmemb < size) { | ||
1440 | errno = ENOMEM; | ||
1441 | return NULL; | ||
1442 | } | ||
1443 | return malloc(size * nmemb); | ||
1444 | } | ||
1445 | |||
1435 | static void * | 1446 | static void * |
1436 | mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill) | 1447 | mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill) |
1437 | { | 1448 | { |