xref: /freebsd/lib/libc/stdlib/reallocarray.3 (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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$
30.\"
31.Dd May 1, 2015
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 nmemb" "size_t size"
43.Sh DESCRIPTION
44The
45.Fn reallocarray
46function is similar to the
47.Fn realloc
48function
49except it operates on
50.Fa nmemb
51members of size
52.Fa size
53and checks for integer overflow in the calculation
54.Fa nmemb
55*
56.Fa size .
57.Sh RETURN VALUES
58The
59.Fn reallocarray
60function returns a pointer to the allocated space; otherwise, a
61.Dv NULL
62pointer is returned and
63.Va errno
64is set to
65.Er ENOMEM .
66.Sh EXAMPLES
67Consider
68.Fn reallocarray
69when there is multiplication in the
70.Fa size
71argument of
72.Fn malloc
73or
74.Fn realloc .
75For example, avoid this common idiom as it may lead to integer overflow:
76.Bd -literal -offset indent
77if ((p = malloc(num * size)) == NULL)
78	err(1, "malloc");
79.Ed
80.Pp
81A drop-in replacement is the
82.Ox
83extension
84.Fn reallocarray :
85.Bd -literal -offset indent
86if ((p = reallocarray(NULL, num, size)) == NULL)
87	err(1, "reallocarray");
88.Ed
89.Pp
90When using
91.Fn realloc ,
92be careful to avoid the following idiom:
93.Bd -literal -offset indent
94size += 50;
95if ((p = realloc(p, size)) == NULL)
96	return (NULL);
97.Ed
98.Pp
99Do not adjust the variable describing how much memory has been allocated
100until the allocation has been successful.
101This can cause aberrant program behavior if the incorrect size value is used.
102In most cases, the above sample will also result in a leak of memory.
103As stated earlier, a return value of
104.Dv NULL
105indicates that the old object still remains allocated.
106Better code looks like this:
107.Bd -literal -offset indent
108newsize = size + 50;
109if ((newp = realloc(p, newsize)) == NULL) {
110	free(p);
111	p = NULL;
112	size = 0;
113	return (NULL);
114}
115p = newp;
116size = newsize;
117.Ed
118.Pp
119As with
120.Fn malloc ,
121it is important to ensure the new size value will not overflow;
122i.e. avoid allocations like the following:
123.Bd -literal -offset indent
124if ((newp = realloc(p, num * size)) == NULL) {
125	...
126.Ed
127.Pp
128Instead, use
129.Fn reallocarray :
130.Bd -literal -offset indent
131if ((newp = reallocarray(p, num, size)) == NULL) {
132	...
133.Ed
134.Sh SEE ALSO
135.Xr realloc 3
136.Sh HISTORY
137The
138.Fn reallocarray
139function first appeared in
140.Ox 5.6
141and
142.Fx 11.0 .
143