diff options
author | tedu <> | 2003-09-18 22:49:13 +0000 |
---|---|---|
committer | tedu <> | 2003-09-18 22:49:13 +0000 |
commit | f971aa97a2c843b38504a5d27260d33fadb82f27 (patch) | |
tree | af38b5d4443f32b5984a6236282cb5c8fe81745f /src | |
parent | 1328054b5f2318ebbd1b4dec83ddf08b1f17777d (diff) | |
download | openbsd-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.3 | 18 |
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 | |||
143 | one must be careful to avoid the following idiom: | 143 | one must be careful to avoid the following idiom: |
144 | .Pp | 144 | .Pp |
145 | .Bd -literal -offset indent | 145 | .Bd -literal -offset indent |
146 | if ((p = realloc(p, nsize)) == NULL) | 146 | size += 50; |
147 | return NULL; | 147 | if ((p = realloc(p, size)) == NULL) |
148 | return (NULL); | ||
148 | .Ed | 149 | .Ed |
149 | .Pp | 150 | .Pp |
150 | In most cases, this will result in a leak of memory. | 151 | Do not adjust the variable describing how much memory has been allocated |
152 | until one knows the allocation has been successful. | ||
153 | This can cause aberrant program behavior if the incorrect size value is used. | ||
154 | In most cases, the above sample will also result in a leak of memory. | ||
151 | As stated earlier, a return value of | 155 | As stated earlier, a return value of |
152 | .Dv NULL | 156 | .Dv NULL |
153 | indicates that the old object still remains allocated. | 157 | indicates that the old object still remains allocated. |
154 | Better code looks like this: | 158 | Better code looks like this: |
155 | .Bd -literal -offset indent | 159 | .Bd -literal -offset indent |
156 | if ((p2 = realloc(p, nsize)) == NULL) { | 160 | newsize = size + 50; |
161 | if ((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 | } |
162 | p = p2; | 167 | p = p2; |
168 | size = newsize; | ||
163 | .Ed | 169 | .Ed |
164 | .Pp | 170 | .Pp |
165 | Malloc will first look for a symbolic link called | 171 | Malloc will first look for a symbolic link called |