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