dbf16d9636f331269fc44fa9123e8be7228cca77
[dragonfly.git] / lib / libc / stdlib / reallocarray.3
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\"
17 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 .\" SUCH DAMAGE.
28 .\"
29 .\" $FreeBSD: head/lib/libc/stdlib/reallocarray.3 282472 2015-05-05 10:44:17Z pluknet $
30 .\"
31 .Dd January 12, 2019
32 .Dt REALLOCARRAY 3
33 .Os
34 .Sh NAME
35 .Nm reallocarray
36 .Nd memory reallocation function
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In stdlib.h
41 .Ft void *
42 .Fn reallocarray "void *ptr" "size_t number" "size_t size"
43 .Sh DESCRIPTION
44 The
45 .Fn reallocarray
46 function is similar to the
47 .Fn realloc
48 function
49 except it operates on
50 .Fa number
51 members of size
52 .Fa size
53 and checks for integer overflow in the calculation
54 .Fa number
55 *
56 .Fa size .
57 .Sh RETURN VALUES
58 The
59 .Fn reallocarray
60 function returns a pointer to the allocated space; otherwise, a
61 .Dv NULL
62 pointer is returned and
63 .Va errno
64 is set to
65 .Er ENOMEM .
66 .Sh EXAMPLES
67 Consider
68 .Fn reallocarray
69 when there is multiplication in the
70 .Fa size
71 argument of
72 .Fn malloc
73 or
74 .Fn realloc .
75 For example, avoid this common idiom as it may lead to integer overflow:
76 .Bd -literal -offset indent
77 if ((p = malloc(num * size)) == NULL)
78         err(1, "malloc");
79 .Ed
80 .Pp
81 A drop-in replacement is the
82 .Ox
83 extension
84 .Fn reallocarray :
85 .Bd -literal -offset indent
86 if ((p = reallocarray(NULL, num, size)) == NULL)
87         err(1, "reallocarray");
88 .Ed
89 .Pp
90 When using
91 .Fn realloc ,
92 be careful to avoid the following idiom:
93 .Bd -literal -offset indent
94 size += 50;
95 if ((p = realloc(p, size)) == NULL)
96         return (NULL);
97 .Ed
98 .Pp
99 Do not adjust the variable describing how much memory has been allocated
100 until the allocation has been successful.
101 This can cause aberrant program behavior if the incorrect size value is used.
102 In most cases, the above sample will also result in a leak of memory.
103 As stated earlier, a return value of
104 .Dv NULL
105 indicates that the old object still remains allocated.
106 Better code looks like this:
107 .Bd -literal -offset indent
108 newsize = size + 50;
109 if ((newp = realloc(p, newsize)) == NULL) {
110         free(p);
111         p = NULL;
112         size = 0;
113         return (NULL);
114 }
115 p = newp;
116 size = newsize;
117 .Ed
118 .Pp
119 As with
120 .Fn malloc ,
121 it is important to ensure the new size value will not overflow;
122 i.e. avoid allocations like the following:
123 .Bd -literal -offset indent
124 if ((newp = realloc(p, num * size)) == NULL) {
125         ...
126 .Ed
127 .Pp
128 Instead, use
129 .Fn reallocarray :
130 .Bd -literal -offset indent
131 if ((newp = reallocarray(p, num, size)) == NULL) {
132         ...
133 .Ed
134 .Sh SEE ALSO
135 .Xr realloc 3
136 .Sh HISTORY
137 The
138 .Fn reallocarray
139 function first appeared in
140 .Ox 5.6
141 and
142 .Fx 11.0 .