summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortedu <>2003-09-18 22:49:13 +0000
committertedu <>2003-09-18 22:49:13 +0000
commitf971aa97a2c843b38504a5d27260d33fadb82f27 (patch)
treeaf38b5d4443f32b5984a6236282cb5c8fe81745f /src
parent1328054b5f2318ebbd1b4dec83ddf08b1f17777d (diff)
downloadopenbsd-f971aa97a2c843b38504a5d27260d33fadb82f27.tar.gz
openbsd-f971aa97a2c843b38504a5d27260d33fadb82f27.tar.bz2
openbsd-f971aa97a2c843b38504a5d27260d33fadb82f27.zip
expand on the realloc no-no section to include adjusting a length before
the allocation. ok deraadt@ markus@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/stdlib/malloc.318
1 files changed, 12 insertions, 6 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 5523798435..d92ebdb2b4 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,7 +30,7 @@
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE. 31.\" SUCH DAMAGE.
32.\" 32.\"
33.\" $OpenBSD: malloc.3,v 1.28 2003/06/02 20:18:37 millert Exp $ 33.\" $OpenBSD: malloc.3,v 1.29 2003/09/18 22:49:13 tedu Exp $
34.\" 34.\"
35.Dd August 27, 1996 35.Dd August 27, 1996
36.Dt MALLOC 3 36.Dt MALLOC 3
@@ -143,23 +143,29 @@ When using
143one must be careful to avoid the following idiom: 143one must be careful to avoid the following idiom:
144.Pp 144.Pp
145.Bd -literal -offset indent 145.Bd -literal -offset indent
146if ((p = realloc(p, nsize)) == NULL) 146size += 50;
147 return NULL; 147if ((p = realloc(p, size)) == NULL)
148 return (NULL);
148.Ed 149.Ed
149.Pp 150.Pp
150In most cases, this will result in a leak of memory. 151Do not adjust the variable describing how much memory has been allocated
152until one knows the allocation has been successful.
153This can cause aberrant program behavior if the incorrect size value is used.
154In most cases, the above sample will also result in a leak of memory.
151As stated earlier, a return value of 155As stated earlier, a return value of
152.Dv NULL 156.Dv NULL
153indicates that the old object still remains allocated. 157indicates that the old object still remains allocated.
154Better code looks like this: 158Better code looks like this:
155.Bd -literal -offset indent 159.Bd -literal -offset indent
156if ((p2 = realloc(p, nsize)) == NULL) { 160newsize = size + 50;
161if ((p2 = realloc(p, newsize)) == NULL) {
157 if (p) 162 if (p)
158 free(p); 163 free(p);
159 p = NULL; 164 p = NULL;
160 return NULL; 165 return (NULL);
161} 166}
162p = p2; 167p = p2;
168size = newsize;
163.Ed 169.Ed
164.Pp 170.Pp
165Malloc will first look for a symbolic link called 171Malloc will first look for a symbolic link called