xref: /minix3/lib/libc/stdlib/reallocarray.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\" $NetBSD: reallocarray.3,v 1.4 2015/07/26 02:29:44 kamil Exp $
2*0a6a1f1dSLionel Sambuc.\"
3*0a6a1f1dSLionel Sambuc.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
4*0a6a1f1dSLionel Sambuc.\" All rights reserved.
5*0a6a1f1dSLionel Sambuc.\"
6*0a6a1f1dSLionel Sambuc.\" Redistribution and use in source and binary forms, with or without
7*0a6a1f1dSLionel Sambuc.\" modification, are permitted provided that the following conditions
8*0a6a1f1dSLionel Sambuc.\" are met:
9*0a6a1f1dSLionel Sambuc.\"
10*0a6a1f1dSLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc.\"    notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc.\"    notice, this list of conditions and the following disclaimer in
14*0a6a1f1dSLionel Sambuc.\"    the documentation and/or other materials provided with the
15*0a6a1f1dSLionel Sambuc.\"    distribution.
16*0a6a1f1dSLionel Sambuc.\"
17*0a6a1f1dSLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*0a6a1f1dSLionel Sambuc.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*0a6a1f1dSLionel Sambuc.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20*0a6a1f1dSLionel Sambuc.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
21*0a6a1f1dSLionel Sambuc.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22*0a6a1f1dSLionel Sambuc.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23*0a6a1f1dSLionel Sambuc.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24*0a6a1f1dSLionel Sambuc.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25*0a6a1f1dSLionel Sambuc.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26*0a6a1f1dSLionel Sambuc.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27*0a6a1f1dSLionel Sambuc.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*0a6a1f1dSLionel Sambuc.\" SUCH DAMAGE.
29*0a6a1f1dSLionel Sambuc.\"
30*0a6a1f1dSLionel Sambuc.Dd February 5, 2015
31*0a6a1f1dSLionel Sambuc.Dt REALLOCARRAY 3
32*0a6a1f1dSLionel Sambuc.Os
33*0a6a1f1dSLionel Sambuc.Sh NAME
34*0a6a1f1dSLionel Sambuc.Nm reallocarray
35*0a6a1f1dSLionel Sambuc.Nd reallocate memory for an array of elements checking for overflow
36*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS
37*0a6a1f1dSLionel Sambuc.Vt #define _OPENBSD_SOURCE
38*0a6a1f1dSLionel Sambuc.In stdlib.h
39*0a6a1f1dSLionel Sambuc.Ft void *
40*0a6a1f1dSLionel Sambuc.Fo reallocarray
41*0a6a1f1dSLionel Sambuc.Fa "void *ptr"
42*0a6a1f1dSLionel Sambuc.Fa "size_t nmemb"
43*0a6a1f1dSLionel Sambuc.Fa "size_t size"
44*0a6a1f1dSLionel Sambuc.Fc
45*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION
46*0a6a1f1dSLionel SambucThe
47*0a6a1f1dSLionel Sambuc.Fn reallocarray
48*0a6a1f1dSLionel Sambucfunction reallocates the pointer
49*0a6a1f1dSLionel Sambuc.Fa ptr
50*0a6a1f1dSLionel Sambucto a size appropriate to handle an allocation of
51*0a6a1f1dSLionel Sambuc.Fa nmemb
52*0a6a1f1dSLionel Sambucelements in an array where each of the array elements is
53*0a6a1f1dSLionel Sambuc.Fa size
54*0a6a1f1dSLionel Sambucbytes using
55*0a6a1f1dSLionel Sambuc.Xr realloc 3
56*0a6a1f1dSLionel Sambucand making sure that overflow does not happen in the multiplication of
57*0a6a1f1dSLionel Sambuc.Dq "nmemb * size" .
58*0a6a1f1dSLionel Sambuc.Pp
59*0a6a1f1dSLionel SambucThis function is provided for source compatibility with
60*0a6a1f1dSLionel Sambuc.Ox
61*0a6a1f1dSLionel Sambucand
62*0a6a1f1dSLionel Sambucits use is discouraged in preference to
63*0a6a1f1dSLionel Sambuc.Xr reallocarr 3 .
64*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES
65*0a6a1f1dSLionel SambucThe
66*0a6a1f1dSLionel Sambuc.Fn reallocarray
67*0a6a1f1dSLionel Sambucfunction will return
68*0a6a1f1dSLionel Sambuc.Dv NULL
69*0a6a1f1dSLionel Sambucif there was overflow or if
70*0a6a1f1dSLionel Sambuc.Xr realloc 3
71*0a6a1f1dSLionel Sambucfailed setting
72*0a6a1f1dSLionel Sambuc.Va errno
73*0a6a1f1dSLionel Sambucto
74*0a6a1f1dSLionel Sambuc.Dv EOVERFLOW
75*0a6a1f1dSLionel Sambucor preserving the value from
76*0a6a1f1dSLionel Sambuc.Xr realloc 3 .
77*0a6a1f1dSLionel Sambuc.Sh SEE ALSO
78*0a6a1f1dSLionel Sambuc.Xr malloc 3 ,
79*0a6a1f1dSLionel Sambuc.Xr realloc 3 ,
80*0a6a1f1dSLionel Sambuc.Xr reallocarr 3
81*0a6a1f1dSLionel Sambuc.Sh STANDARDS
82*0a6a1f1dSLionel Sambuc.Fn reallocarray
83*0a6a1f1dSLionel Sambucis an
84*0a6a1f1dSLionel Sambuc.Ox
85*0a6a1f1dSLionel Sambucextension.
86*0a6a1f1dSLionel Sambuc.Sh HISTORY
87*0a6a1f1dSLionel SambucThe
88*0a6a1f1dSLionel Sambuc.Fn reallocarray
89*0a6a1f1dSLionel Sambucfunction first appeared in
90*0a6a1f1dSLionel Sambuc.Ox 5.6 .
91*0a6a1f1dSLionel Sambuc.Fn reallocarray
92*0a6a1f1dSLionel Sambucwas redesigned in
93*0a6a1f1dSLionel Sambuc.Nx 8
94*0a6a1f1dSLionel Sambucas
95*0a6a1f1dSLionel Sambuc.Fn reallocarr 3 .
96*0a6a1f1dSLionel SambucFor compatibility reasons it's available since
97*0a6a1f1dSLionel Sambuc.Nx 8
98*0a6a1f1dSLionel Sambucin the
99*0a6a1f1dSLionel Sambuc.Vt _OPENBSD_SOURCE
100*0a6a1f1dSLionel Sambucnamespace.
101*0a6a1f1dSLionel Sambuc.Sh CAVEATS
102*0a6a1f1dSLionel SambucThe
103*0a6a1f1dSLionel Sambuc.Fn reallocarray
104*0a6a1f1dSLionel Sambucfunction was designed to facilitate safe,
105*0a6a1f1dSLionel Sambucrobust programming and overcome the shortcomings of the
106*0a6a1f1dSLionel Sambuc.Xr malloc 3
107*0a6a1f1dSLionel Sambucand
108*0a6a1f1dSLionel Sambuc.Xr realloc 3
109*0a6a1f1dSLionel Sambucfunctions by centralizing the overflow check in the multiplication of
110*0a6a1f1dSLionel Sambuc.Fa nmemb
111*0a6a1f1dSLionel Sambucand
112*0a6a1f1dSLionel Sambuc.Fa size .
113*0a6a1f1dSLionel Sambuc.Pp
114*0a6a1f1dSLionel SambucThere are still portability issues (it does not solve
115*0a6a1f1dSLionel Sambucthe
116*0a6a1f1dSLionel Sambuc.Dv 0
117*0a6a1f1dSLionel Sambucsized allocation return ambiguity in the C standard: does
118*0a6a1f1dSLionel Sambuc.Fn reallocarray
119*0a6a1f1dSLionel Sambucreturn
120*0a6a1f1dSLionel Sambuc.Dv NULL
121*0a6a1f1dSLionel Sambucor a unique pointer to memory that cannot be accessed? Does a
122*0a6a1f1dSLionel Sambuc.Dv NULL
123*0a6a1f1dSLionel Sambucmean that an error occurred, and can someone check
124*0a6a1f1dSLionel Sambuc.Dv errno
125*0a6a1f1dSLionel Sambucin that case to find out what happened?).
126*0a6a1f1dSLionel Sambuc.Pp
127*0a6a1f1dSLionel SambucFor this reason
128*0a6a1f1dSLionel Sambuc.Nx
129*0a6a1f1dSLionel Sambucdecided to go with an alternative implementation, and created
130*0a6a1f1dSLionel Sambuc.Xr reallocarr 3 .
131