Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src Introduce reallocarr(3) for easy (re)allocation of memory wi...
details: https://anonhg.NetBSD.org/src/rev/8480cb52ffab
branches: trunk
changeset: 336206:8480cb52ffab
user: joerg <joerg%NetBSD.org@localhost>
date: Tue Feb 17 20:29:20 2015 +0000
description:
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.
diffstat:
distrib/sets/lists/comp/mi | 5 ++-
include/stdlib.h | 4 +-
lib/libc/include/namespace.h | 3 +-
lib/libc/stdlib/Makefile.inc | 6 +-
lib/libc/stdlib/reallocarr.3 | 67 +++++++++++++++++++++++++++++++++++++
lib/libc/stdlib/reallocarr.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 157 insertions(+), 6 deletions(-)
diffs (248 lines):
diff -r 7bc57ad80e35 -r 8480cb52ffab distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi Tue Feb 17 15:14:28 2015 +0000
+++ b/distrib/sets/lists/comp/mi Tue Feb 17 20:29:20 2015 +0000
@@ -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
diff -r 7bc57ad80e35 -r 8480cb52ffab include/stdlib.h
--- a/include/stdlib.h Tue Feb 17 15:14:28 2015 +0000
+++ b/include/stdlib.h Tue Feb 17 20:29:20 2015 +0000
@@ -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 @@
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 */
diff -r 7bc57ad80e35 -r 8480cb52ffab lib/libc/include/namespace.h
--- a/lib/libc/include/namespace.h Tue Feb 17 15:14:28 2015 +0000
+++ b/lib/libc/include/namespace.h Tue Feb 17 20:29:20 2015 +0000
@@ -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
diff -r 7bc57ad80e35 -r 8480cb52ffab lib/libc/stdlib/Makefile.inc
--- a/lib/libc/stdlib/Makefile.inc Tue Feb 17 15:14:28 2015 +0000
+++ b/lib/libc/stdlib/Makefile.inc Tue Feb 17 20:29:20 2015 +0000
@@ -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 @@
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 @@
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 \
diff -r 7bc57ad80e35 -r 8480cb52ffab lib/libc/stdlib/reallocarr.3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdlib/reallocarr.3 Tue Feb 17 20:29:20 2015 +0000
@@ -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.
diff -r 7bc57ad80e35 -r 8480cb52ffab lib/libc/stdlib/reallocarr.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/stdlib/reallocarr.c Tue Feb 17 20:29:20 2015 +0000
@@ -0,0 +1,78 @@
+/* $NetBSD: reallocarr.c,v 1.1 2015/02/17 20:29:21 joerg Exp $ */
+
+/*-
+ * Copyright (c) 2015 Joerg Sonnenberger <joerg%NetBSD.org@localhost>.
+ * 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;
+}
Home |
Main Index |
Thread Index |
Old Index