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