diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1998-08-15 20:32:03 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1998-08-15 20:32:03 +0000 |
commit | 05f405bf353395695619cad53097ca6ba07ace64 (patch) | |
tree | 4b8b84e74178a28b5d93d3eed7b5bdd93ad6c99f /lib | |
parent | f0507b9ffcb2da069000ab407839b071c3fd3f05 (diff) |
document the common misuse of realloc
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdlib/malloc.3 | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3 index 42b69f038fc..d5f8837ec23 100644 --- a/lib/libc/stdlib/malloc.3 +++ b/lib/libc/stdlib/malloc.3 @@ -33,7 +33,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: malloc.3,v 1.8 1998/04/28 07:36:47 deraadt Exp $ +.\" $OpenBSD: malloc.3,v 1.9 1998/08/15 20:32:02 deraadt Exp $ .\" .Dd August 27, 1996 .Dt MALLOC 3 @@ -106,7 +106,7 @@ to the size specified by The contents of the object are unchanged up to the lesser of the new and old sizes. If the new size is larger, the value of the newly allocated portion -of the object is indeterminate. +of the object is indeterminate and uninitialized. If .Fa ptr is a null pointer, the @@ -125,6 +125,30 @@ is zero and is not a null pointer, the object it points to is freed and a new zero size object is returned. .Pp +When using +.Fn realloc +one must be careful to avoid the following idiom: +.Pp +.Bd -literal -offset indent +if ((p = realloc(p, nsize)) == NULL) + return NULL; +.Ed +.Pp +In most cases, this will result in a leak of memory. +As stated earlier, a return value of +.Fa NULL +indicates that the old object still remains allocated. +Better code looks like this: +.Bd -literal -offset indent +if ((p2 = realloc(p, nsize)) == NULL) { + if (p) + free(p); + p = NULL; + return NULL; +} +p = p2; +.Ed +.Pp Malloc will first look for a symbolic link called .Pa /etc/malloc.conf and next check the environment for a variable called |