summaryrefslogtreecommitdiff
path: root/src/lib/libc
diff options
context:
space:
mode:
authorray <>2006-03-26 19:56:08 +0000
committerray <>2006-03-26 19:56:08 +0000
commite9b518847b88ec21d7f09693543570b157debf5b (patch)
treeb4633b630328840de04ebcbb3ccfba8fb158cd6f /src/lib/libc
parentbb8d758ce8162eb6175ee6329cbc8d33d3515016 (diff)
downloadopenbsd-e9b518847b88ec21d7f09693543570b157debf5b.tar.gz
openbsd-e9b518847b88ec21d7f09693543570b157debf5b.tar.bz2
openbsd-e9b518847b88ec21d7f09693543570b157debf5b.zip
Add warning about malloc(num * size) and recommend calloc() instead,
or if malloc must be used suggest check. Get rid of "one". OK deraadt@ and jmc@, OK kjell@ to earlier version with "one"s.
Diffstat (limited to 'src/lib/libc')
-rw-r--r--src/lib/libc/stdlib/malloc.345
1 files changed, 40 insertions, 5 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index 3bb4ad8326..24e6b3bc53 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.42 2006/01/18 06:36:05 jakemsr Exp $ 33.\" $OpenBSD: malloc.3,v 1.43 2006/03/26 19:56:08 ray Exp $
34.\" 34.\"
35.Dd August 27, 1996 35.Dd August 27, 1996
36.Dt MALLOC 3 36.Dt MALLOC 3
@@ -83,6 +83,29 @@ The minimum size of the protection on each object is suitably aligned and
83sized as previously stated, but the protection may extend further depending 83sized as previously stated, but the protection may extend further depending
84on where in a protected zone the object lands. 84on where in a protected zone the object lands.
85.Pp 85.Pp
86When using
87.Fn malloc
88be careful to avoid the following idiom:
89.Bd -literal -offset indent
90if ((p = malloc(num * size)) == NULL)
91 err(1, "malloc");
92.Ed
93.Pp
94The multiplication may lead to an integer overflow.
95To avoid this,
96.Fn calloc
97is recommended.
98.Pp
99If
100.Fn malloc
101must be used, be sure to test for overflow:
102.Bd -literal -offset indent
103if (num && size && SIZE_T_MAX / num < size) {
104 errno = ENOMEM;
105 err(1, "overflow");
106}
107.Ed
108.Pp
86The 109The
87.Fn calloc 110.Fn calloc
88function allocates space for an array of 111function allocates space for an array of
@@ -90,6 +113,10 @@ function allocates space for an array of
90objects, each of whose size is 113objects, each of whose size is
91.Fa size . 114.Fa size .
92The space is initialized to all bits zero. 115The space is initialized to all bits zero.
116The use of
117.Fn calloc
118is strongly encouraged when allocating multiple sized objects
119in order to avoid possible integer overflows.
93.Pp 120.Pp
94The 121The
95.Fn free 122.Fn free
@@ -140,7 +167,7 @@ object is returned.
140.Pp 167.Pp
141When using 168When using
142.Fn realloc 169.Fn realloc
143one must be careful to avoid the following idiom: 170be careful to avoid the following idiom:
144.Bd -literal -offset indent 171.Bd -literal -offset indent
145size += 50; 172size += 50;
146if ((p = realloc(p, size)) == NULL) 173if ((p = realloc(p, size)) == NULL)
@@ -148,7 +175,7 @@ if ((p = realloc(p, size)) == NULL)
148.Ed 175.Ed
149.Pp 176.Pp
150Do not adjust the variable describing how much memory has been allocated 177Do not adjust the variable describing how much memory has been allocated
151until one knows the allocation has been successful. 178until the allocation has been successful.
152This can cause aberrant program behavior if the incorrect size value is used. 179This can cause aberrant program behavior if the incorrect size value is used.
153In most cases, the above sample will also result in a leak of memory. 180In most cases, the above sample will also result in a leak of memory.
154As stated earlier, a return value of 181As stated earlier, a return value of
@@ -167,6 +194,15 @@ p = newp;
167size = newsize; 194size = newsize;
168.Ed 195.Ed
169.Pp 196.Pp
197As with
198.Fn malloc
199it is important to ensure the new size value will not overflow;
200i.e. avoid allocations like the following:
201.Bd -literal -offset indent
202if ((newp = realloc(p, num * size)) == NULL) {
203 ...
204.Ed
205.Pp
170Malloc will first look for a symbolic link called 206Malloc will first look for a symbolic link called
171.Pa /etc/malloc.conf 207.Pa /etc/malloc.conf
172and next check the environment for a variable called 208and next check the environment for a variable called
@@ -255,8 +291,7 @@ Reduce the size of the cache by a factor of two.
255Double the size of the cache by a factor of two. 291Double the size of the cache by a factor of two.
256.El 292.El
257.Pp 293.Pp
258So to set a systemwide reduction of cache size and coredumps on problems 294So to set a systemwide reduction of cache size and coredumps on problems:
259one would:
260.Li ln -s 'A<' /etc/malloc.conf 295.Li ln -s 'A<' /etc/malloc.conf
261.Pp 296.Pp
262The 297The