summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorderaadt <>1998-08-15 20:32:02 +0000
committerderaadt <>1998-08-15 20:32:02 +0000
commit52dd1957a7eb7170023ae5f987f40b186406bd4b (patch)
treedd0f21e736a6d7ae32eea8d6c3fa900f07c18529 /src
parent2f3712534799d2184deb6221d3f4d1f8d9098442 (diff)
downloadopenbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.tar.gz
openbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.tar.bz2
openbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.zip
document the common misuse of realloc
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/malloc.328
1 files changed, 26 insertions, 2 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 42b69f038f..d5f8837ec2 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -33,7 +33,7 @@
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE. 34.\" SUCH DAMAGE.
35.\" 35.\"
36.\" $OpenBSD: malloc.3,v 1.8 1998/04/28 07:36:47 deraadt Exp $ 36.\" $OpenBSD: malloc.3,v 1.9 1998/08/15 20:32:02 deraadt Exp $
37.\" 37.\"
38.Dd August 27, 1996 38.Dd August 27, 1996
39.Dt MALLOC 3 39.Dt MALLOC 3
@@ -106,7 +106,7 @@ to the size specified by
106The contents of the object are unchanged up to the lesser 106The contents of the object are unchanged up to the lesser
107of the new and old sizes. 107of the new and old sizes.
108If the new size is larger, the value of the newly allocated portion 108If the new size is larger, the value of the newly allocated portion
109of the object is indeterminate. 109of the object is indeterminate and uninitialized.
110If 110If
111.Fa ptr 111.Fa ptr
112is a null pointer, the 112is a null pointer, the
@@ -125,6 +125,30 @@ is zero and
125is not a null pointer, the object it points to is freed and a new zero size 125is not a null pointer, the object it points to is freed and a new zero size
126object is returned. 126object is returned.
127.Pp 127.Pp
128When using
129.Fn realloc
130one must be careful to avoid the following idiom:
131.Pp
132.Bd -literal -offset indent
133if ((p = realloc(p, nsize)) == NULL)
134 return NULL;
135.Ed
136.Pp
137In most cases, this will result in a leak of memory.
138As stated earlier, a return value of
139.Fa NULL
140indicates that the old object still remains allocated.
141Better code looks like this:
142.Bd -literal -offset indent
143if ((p2 = realloc(p, nsize)) == NULL) {
144 if (p)
145 free(p);
146 p = NULL;
147 return NULL;
148}
149p = p2;
150.Ed
151.Pp
128Malloc will first look for a symbolic link called 152Malloc will first look for a symbolic link called
129.Pa /etc/malloc.conf 153.Pa /etc/malloc.conf
130and next check the environment for a variable called 154and next check the environment for a variable called