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