summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorotto <>2017-04-13 18:32:55 +0000
committerotto <>2017-04-13 18:32:55 +0000
commitc8e0fc6335c50b963cf789e50fa6dd954ee974b0 (patch)
tree3a288a7b1dc6c5346a27f50bc19e963b0aa87efd /src/lib
parent0f3f0734ef5aa0887305bde6762a8cbeb7d54695 (diff)
downloadopenbsd-c8e0fc6335c50b963cf789e50fa6dd954ee974b0.tar.gz
openbsd-c8e0fc6335c50b963cf789e50fa6dd954ee974b0.tar.bz2
openbsd-c8e0fc6335c50b963cf789e50fa6dd954ee974b0.zip
allow clearing less than allocated and document freezero(3) better
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/stdlib/malloc.329
-rw-r--r--src/lib/libc/stdlib/malloc.c10
2 files changed, 26 insertions, 13 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
index c09b4756fa..602787532e 100644
--- a/src/lib/libc/stdlib/malloc.3
+++ b/src/lib/libc/stdlib/malloc.3
@@ -30,9 +30,9 @@
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.111 2017/04/10 06:31:31 jmc Exp $ 33.\" $OpenBSD: malloc.3,v 1.112 2017/04/13 18:32:55 otto Exp $
34.\" 34.\"
35.Dd $Mdocdate: April 10 2017 $ 35.Dd $Mdocdate: April 13 2017 $
36.Dt MALLOC 3 36.Dt MALLOC 3
37.Os 37.Os
38.Sh NAME 38.Sh NAME
@@ -67,7 +67,9 @@ The standard functions
67.Fn calloc , 67.Fn calloc ,
68and 68and
69.Fn realloc 69.Fn realloc
70allocate memory space. 70allocate
71.Em objects ,
72regions of memory to store values.
71The 73The
72.Fn malloc 74.Fn malloc
73function allocates uninitialized space for an object of 75function allocates uninitialized space for an object of
@@ -94,6 +96,12 @@ function changes the size of the object pointed to by
94to 96to
95.Fa size 97.Fa size
96bytes and returns a pointer to the (possibly moved) object. 98bytes and returns a pointer to the (possibly moved) object.
99If
100.Fa ptr
101is not
102.Dv NULL ,
103it must be a pointer returned by an earlier call to an allocation or
104reallocation function that was not freed in between.
97The contents of the object are unchanged up to the lesser 105The contents of the object are unchanged up to the lesser
98of the new and old sizes. 106of the new and old sizes.
99If the new size is larger, the value of the newly allocated portion 107If the new size is larger, the value of the newly allocated portion
@@ -183,8 +191,7 @@ The
183.Fn freezero 191.Fn freezero
184function is similar to the 192function is similar to the
185.Fn free 193.Fn free
186function except it ensures the memory being deallocated is explicitly 194function except it ensures memory is explicitly discarded.
187discarded.
188If 195If
189.Fa ptr 196.Fa ptr
190is 197is
@@ -196,9 +203,15 @@ is not
196.Dv NULL , 203.Dv NULL ,
197the 204the
198.Fa size 205.Fa size
199argument must be the size of the earlier allocation that returned 206argument must be equal or smaller than the size of the earlier allocation
200.Fa ptr , 207that returned
201otherwise the behaviour is undefined. 208.Fa ptr .
209.Fn freezero
210guarantees the memory range starting at
211.Fa ptr
212with length
213.Fa size
214is discarded while deallocating the whole object originally allocated.
202.Sh RETURN VALUES 215.Sh RETURN VALUES
203Upon successful completion, the allocation functions 216Upon successful completion, the allocation functions
204return a pointer to the allocated space; otherwise, a 217return a pointer to the allocated space; otherwise, a
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 07c73ca774..ecac7ddfe9 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.220 2017/04/10 05:45:02 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.221 2017/04/13 18:32:55 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> 4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
@@ -1340,15 +1340,15 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz)
1340 uint32_t chunknum = 1340 uint32_t chunknum =
1341 find_chunknum(pool, r, p, 0); 1341 find_chunknum(pool, r, p, 0);
1342 1342
1343 if (info->bits[info->offset + chunknum] != 1343 if (info->bits[info->offset + chunknum] <
1344 argsz) 1344 argsz)
1345 wrterror(pool, "recorded old size %hu" 1345 wrterror(pool, "recorded old size %hu"
1346 " != %zu", 1346 " < %zu",
1347 info->bits[info->offset + chunknum], 1347 info->bits[info->offset + chunknum],
1348 argsz); 1348 argsz);
1349 } 1349 }
1350 } else if (argsz != sz - mopts.malloc_guard) 1350 } else if (sz - mopts.malloc_guard < argsz)
1351 wrterror(pool, "recorded old size %zu != %zu", 1351 wrterror(pool, "recorded old size %zu < %zu",
1352 sz - mopts.malloc_guard, argsz); 1352 sz - mopts.malloc_guard, argsz);
1353 } 1353 }
1354 if (sz > MALLOC_MAXCHUNK) { 1354 if (sz > MALLOC_MAXCHUNK) {