diff options
| author | deraadt <> | 1998-08-15 20:32:02 +0000 |
|---|---|---|
| committer | deraadt <> | 1998-08-15 20:32:02 +0000 |
| commit | 52dd1957a7eb7170023ae5f987f40b186406bd4b (patch) | |
| tree | dd0f21e736a6d7ae32eea8d6c3fa900f07c18529 /src/lib/libc/stdlib | |
| parent | 2f3712534799d2184deb6221d3f4d1f8d9098442 (diff) | |
| download | openbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.tar.gz openbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.tar.bz2 openbsd-52dd1957a7eb7170023ae5f987f40b186406bd4b.zip | |
document the common misuse of realloc
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 28 |
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 | |||
| 106 | The contents of the object are unchanged up to the lesser | 106 | The contents of the object are unchanged up to the lesser |
| 107 | of the new and old sizes. | 107 | of the new and old sizes. |
| 108 | If the new size is larger, the value of the newly allocated portion | 108 | If the new size is larger, the value of the newly allocated portion |
| 109 | of the object is indeterminate. | 109 | of the object is indeterminate and uninitialized. |
| 110 | If | 110 | If |
| 111 | .Fa ptr | 111 | .Fa ptr |
| 112 | is a null pointer, the | 112 | is a null pointer, the |
| @@ -125,6 +125,30 @@ is zero and | |||
| 125 | is not a null pointer, the object it points to is freed and a new zero size | 125 | is not a null pointer, the object it points to is freed and a new zero size |
| 126 | object is returned. | 126 | object is returned. |
| 127 | .Pp | 127 | .Pp |
| 128 | When using | ||
| 129 | .Fn realloc | ||
| 130 | one must be careful to avoid the following idiom: | ||
| 131 | .Pp | ||
| 132 | .Bd -literal -offset indent | ||
| 133 | if ((p = realloc(p, nsize)) == NULL) | ||
| 134 | return NULL; | ||
| 135 | .Ed | ||
| 136 | .Pp | ||
| 137 | In most cases, this will result in a leak of memory. | ||
| 138 | As stated earlier, a return value of | ||
| 139 | .Fa NULL | ||
| 140 | indicates that the old object still remains allocated. | ||
| 141 | Better code looks like this: | ||
| 142 | .Bd -literal -offset indent | ||
| 143 | if ((p2 = realloc(p, nsize)) == NULL) { | ||
| 144 | if (p) | ||
| 145 | free(p); | ||
| 146 | p = NULL; | ||
| 147 | return NULL; | ||
| 148 | } | ||
| 149 | p = p2; | ||
| 150 | .Ed | ||
| 151 | .Pp | ||
| 128 | Malloc will first look for a symbolic link called | 152 | Malloc will first look for a symbolic link called |
| 129 | .Pa /etc/malloc.conf | 153 | .Pa /etc/malloc.conf |
| 130 | and next check the environment for a variable called | 154 | and next check the environment for a variable called |
