summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/Makefile.inc13
-rw-r--r--src/lib/libc/stdlib/malloc.c13
-rw-r--r--src/lib/libc/stdlib/reallocarray.c38
3 files changed, 46 insertions, 18 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 83b42c34e3..80c3e5f5a1 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.52 2014/04/22 14:26:26 tedu Exp $ 1# $OpenBSD: Makefile.inc,v 1.53 2014/05/08 21:43:49 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
@@ -6,11 +6,12 @@
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ 6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ 7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ 8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \
9 lldiv.c lsearch.c malloc.c merge.c posix_pty.c qsort.c radixsort.c \ 9 lldiv.c lsearch.c malloc.c reallocarray.c merge.c posix_pty.c \
10 rand.c random.c realpath.c setenv.c strtoimax.c strtol.c \ 10 qsort.c radixsort.c rand.c random.c realpath.c setenv.c strtoimax.c \
11 strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ 11 strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \
12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ 12 system.c tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c \
13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c 13 lcong48.c lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c \
14 qdiv.c _Exit.c
14 15
15.if (${MACHINE_CPU} == "i386") 16.if (${MACHINE_CPU} == "i386")
16SRCS+= abs.S div.S labs.S ldiv.S 17SRCS+= abs.S div.S labs.S ldiv.S
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 6f2a48ba4c..a015fa229b 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.160 2014/05/07 20:07:59 halex Exp $ */ 1/* $OpenBSD: malloc.c,v 1.161 2014/05/08 21:43:49 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>
@@ -1424,17 +1424,6 @@ calloc(size_t nmemb, size_t size)
1424 return r; 1424 return r;
1425} 1425}
1426 1426
1427void *
1428reallocarray(void *optr, size_t nmemb, size_t size)
1429{
1430 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1431 nmemb > 0 && SIZE_MAX / nmemb < size) {
1432 errno = ENOMEM;
1433 return NULL;
1434 }
1435 return realloc(optr, size * nmemb);
1436}
1437
1438static void * 1427static void *
1439mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill) 1428mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill)
1440{ 1429{
diff --git a/src/lib/libc/stdlib/reallocarray.c b/src/lib/libc/stdlib/reallocarray.c
new file mode 100644
index 0000000000..7accd99e00
--- /dev/null
+++ b/src/lib/libc/stdlib/reallocarray.c
@@ -0,0 +1,38 @@
1/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */
2/*
3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/types.h>
19#include <errno.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23/*
24 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
25 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
26 */
27#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
28
29void *
30reallocarray(void *optr, size_t nmemb, size_t size)
31{
32 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
33 nmemb > 0 && SIZE_MAX / nmemb < size) {
34 errno = ENOMEM;
35 return NULL;
36 }
37 return realloc(optr, size * nmemb);
38}