Module Name: src
Committed By: joerg
Date: Tue Feb 17 20:29:21 UTC 2015
Modified Files:
src/distrib/sets/lists/comp: mi
src/include: stdlib.h
src/lib/libc/include: namespace.h
src/lib/libc/stdlib: Makefile.inc
Added Files:
src/lib/libc/stdlib: reallocarr.3 reallocarr.c
Log Message:
Introduce reallocarr(3) for easy (re)allocation of memory with overflow
checks. Compared to OpenBSD's reallocarray, makes it easier to avoid memory
leaks on allocation failures and it doesn't depend on malloc(0) != NULL
for correct error checking. Compared to plain realloc, it also avoids
the problem of intermediate integer overflows. The trade-off is the use
of void * to side step C type system with regard to generic pointer to
pointer.
To generate a diff of this commit:
cvs rdiff -u -r1.1946 -r1.1947 src/distrib/sets/lists/comp/mi
cvs rdiff -u -r1.113 -r1.114 src/include/stdlib.h
cvs rdiff -u -r1.176 -r1.177 src/lib/libc/include/namespace.h
cvs rdiff -u -r1.88 -r1.89 src/lib/libc/stdlib/Makefile.inc
cvs rdiff -u -r0 -r1.1 src/lib/libc/stdlib/reallocarr.3 \
src/lib/libc/stdlib/reallocarr.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/sets/lists/comp/mi
diff -u src/distrib/sets/lists/comp/mi:1.1946 src/distrib/sets/lists/comp/mi:1.1947
--- src/distrib/sets/lists/comp/mi:1.1946 Tue Feb 10 19:11:52 2015
+++ src/distrib/sets/lists/comp/mi Tue Feb 17 20:29:20 2015
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1946 2015/02/10 19:11:52 rjs Exp $
+# $NetBSD: mi,v 1.1947 2015/02/17 20:29:20 joerg Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -8766,6 +8766,7 @@
./usr/share/man/cat3/re_exec.0 comp-c-catman .cat
./usr/share/man/cat3/readdir.0 comp-c-catman .cat
./usr/share/man/cat3/readdir_r.0 comp-c-catman .cat
+./usr/share/man/cat3/reallocarr.0 comp-c-catman .cat
./usr/share/man/cat3/reallocarray.0 comp-c-catman .cat
./usr/share/man/cat3/realloc.0 comp-c-catman .cat
./usr/share/man/cat3/realpath.0 comp-c-catman .cat
@@ -15606,6 +15607,7 @@
./usr/share/man/html3/re_exec.html comp-c-htmlman html
./usr/share/man/html3/readdir.html comp-c-htmlman html
./usr/share/man/html3/readdir_r.html comp-c-htmlman html
+./usr/share/man/html3/reallocarr.html comp-c-htmlman html
./usr/share/man/html3/reallocarray.html comp-c-htmlman html
./usr/share/man/html3/realloc.html comp-c-htmlman html
./usr/share/man/html3/realpath.html comp-c-htmlman html
@@ -22445,6 +22447,7 @@
./usr/share/man/man3/re_exec.3 comp-c-man .man
./usr/share/man/man3/readdir.3 comp-c-man .man
./usr/share/man/man3/readdir_r.3 comp-c-man .man
+./usr/share/man/man3/reallocarr.3 comp-c-man .man
./usr/share/man/man3/reallocarray.3 comp-c-man .man
./usr/share/man/man3/realloc.3 comp-c-man .man
./usr/share/man/man3/realpath.3 comp-c-man .man
Index: src/include/stdlib.h
diff -u src/include/stdlib.h:1.113 src/include/stdlib.h:1.114
--- src/include/stdlib.h:1.113 Thu Feb 5 16:05:46 2015
+++ src/include/stdlib.h Tue Feb 17 20:29:20 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: stdlib.h,v 1.113 2015/02/05 16:05:46 christos Exp $ */
+/* $NetBSD: stdlib.h,v 1.114 2015/02/17 20:29:20 joerg Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -337,6 +337,8 @@ int l64a_r(long, char *, int);
size_t shquote(const char *, char *, size_t);
size_t shquotev(int, char * const *, char *, size_t);
+
+int reallocarr(void *, size_t, size_t);
#endif /* _NETBSD_SOURCE */
#endif /* _POSIX_C_SOURCE || _XOPEN_SOURCE || _NETBSD_SOURCE */
Index: src/lib/libc/include/namespace.h
diff -u src/lib/libc/include/namespace.h:1.176 src/lib/libc/include/namespace.h:1.177
--- src/lib/libc/include/namespace.h:1.176 Fri Sep 26 19:28:03 2014
+++ src/lib/libc/include/namespace.h Tue Feb 17 20:29:21 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: namespace.h,v 1.176 2014/09/26 19:28:03 christos Exp $ */
+/* $NetBSD: namespace.h,v 1.177 2015/02/17 20:29:21 joerg Exp $ */
/*-
* Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
@@ -547,6 +547,7 @@
#define readdir _readdir
#define readdir_r _readdir_r
#define readlink _readlink
+#define reallocarr _reallocarr
#define realpath _realpath
#define regcomp _regcomp
#define regerror _regerror
Index: src/lib/libc/stdlib/Makefile.inc
diff -u src/lib/libc/stdlib/Makefile.inc:1.88 src/lib/libc/stdlib/Makefile.inc:1.89
--- src/lib/libc/stdlib/Makefile.inc:1.88 Thu Feb 5 20:02:28 2015
+++ src/lib/libc/stdlib/Makefile.inc Tue Feb 17 20:29:21 2015
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile.inc,v 1.88 2015/02/05 20:02:28 christos Exp $
+# $NetBSD: Makefile.inc,v 1.89 2015/02/17 20:29:21 joerg Exp $
# from: @(#)Makefile.inc 8.3 (Berkeley) 2/4/95
# stdlib sources
@@ -12,7 +12,7 @@ SRCS+= _env.c _rand48.c \
lcong48.c lrand48.c lsearch.c merge.c mi_vector_hash.c mrand48.c \
nrand48.c putenv.c qabs.c qdiv.c qsort.c posix_openpt.c pty.c \
quick_exit.c radixsort.c rand.c rand_r.c random.c remque.c \
- reallocarray.c seed48.c setenv.c srand48.c strsuftoll.c \
+ reallocarr.c reallocarray.c seed48.c setenv.c srand48.c strsuftoll.c \
strtoi.c strtou.c strtonum.c \
strtoimax.c strtol.c strtoll.c strtoq.c strtoul.c strtoull.c \
strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c \
@@ -52,7 +52,7 @@ MAN+= a64l.3 abort.3 abs.3 alloca.3 atex
malloc.3 memory.3 mi_vector_hash.3 \
posix_memalign.3 posix_openpt.3 ptsname.3 \
qabs.3 qdiv.3 quick_exit.3 qsort.3 \
- radixsort.3 rand48.3 rand.3 random.3 reallocarray.3 \
+ radixsort.3 rand48.3 rand.3 random.3 reallocarr.3 reallocarray.3 \
strfmon.3 strsuftoll.3 strtod.3 strtol.3 strtoul.3 strtonum.3 \
system.3 \
tsearch.3 \
Added files:
Index: src/lib/libc/stdlib/reallocarr.3
diff -u /dev/null src/lib/libc/stdlib/reallocarr.3:1.1
--- /dev/null Tue Feb 17 20:29:21 2015
+++ src/lib/libc/stdlib/reallocarr.3 Tue Feb 17 20:29:21 2015
@@ -0,0 +1,67 @@
+.\" $NetBSD: reallocarr.3,v 1.1 2015/02/17 20:29:21 joerg Exp $
+.\"
+.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\"
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in
+.\" the documentation and/or other materials provided with the
+.\" distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.Dd February 5, 2015
+.Dt REALLOCARR 3
+.Os
+.Sh NAME
+.Nm reallocarr
+.Nd reallocate array
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft int
+.Fo reallocarr
+.Fa "void *ptr"
+.Fa "size_t num"
+.Fa "size_t size"
+.Fc
+.Sh DESCRIPTION
+The
+.Nm
+function reallocates the memory in
+.Fa *ptr .
+.Sh RETURN VALUES
+On successful completion,
+.Fn
+returns 0 and updates
+.Fa *ptr .
+Otherwise, an error code (see
+.Xr errno 7 )
+is returned and
+.Fa *ptr
+and the referenced memory is unmodified.
+.Sh SEE ALSO
+.Xr calloc 3
+.Sh HISTORY
+.Nm
+first appeared in
+.Nx 7.0 .
+.Ox introduced the
+.Xr reallocarray 3
+function for the same purpose, but the interface makes it difficult
+to correctly handle zero-sized allocations.
Index: src/lib/libc/stdlib/reallocarr.c
diff -u /dev/null src/lib/libc/stdlib/reallocarr.c:1.1
--- /dev/null Tue Feb 17 20:29:21 2015
+++ src/lib/libc/stdlib/reallocarr.c Tue Feb 17 20:29:21 2015
@@ -0,0 +1,78 @@
+/* $NetBSD: reallocarr.c,v 1.1 2015/02/17 20:29:21 joerg Exp $ */
+
+/*-
+ * Copyright (c) 2015 Joerg Sonnenberger <[email protected]>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: reallocarr.c,v 1.1 2015/02/17 20:29:21 joerg Exp $");
+
+#include "namespace.h"
+#include <errno.h>
+/* Old POSIX has SIZE_MAX in limits.h */
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+__CTASSERT(65535 < SIZE_MAX / 65535);
+
+#ifdef _LIBC
+#ifdef __weak_alias
+__weak_alias(reallocarr, _reallocarr)
+#endif
+#endif
+
+int
+reallocarr(void *ptr, size_t num, size_t size)
+{
+ int saved_errno, result;
+ void *optr;
+ void *nptr;
+
+ memcpy(&optr, ptr, sizeof(ptr));
+ saved_errno = errno;
+ if (num == 0 || size == 0) {
+ free(optr);
+ nptr = NULL;
+ memcpy(ptr, &nptr, sizeof(ptr));
+ errno = saved_errno;
+ return 0;
+ }
+ if ((num >= 65535 || size >= 65535) && num > SIZE_MAX / size)
+ return EOVERFLOW;
+ nptr = realloc(optr, num * size);
+ if (nptr == NULL) {
+ result = errno;
+ } else {
+ result = 0;
+ memcpy(ptr, &nptr, sizeof(ptr));
+ }
+ errno = saved_errno;
+ return result;
+}