summaryrefslogtreecommitdiff
path: root/libc/stdlib/malloc/memalign.c
diff options
context:
space:
mode:
authorKjetil Oftedal <oftedal@gmail.com>2019-09-25 09:59:46 +0200
committerWaldemar Brodkorb <wbx@openadk.org>2019-09-30 19:29:07 +0200
commit3538ba34e0e415105d3fe235605e6dba1597ad98 (patch)
tree2d4986aa38bf105c73657e0206f2dbd5c1864a87 /libc/stdlib/malloc/memalign.c
parent7ed8c421f71f89978da0ba30b19df245c7c02e39 (diff)
malloc: Add missing locks for some paths (valloc/memalign/posix_memalign)
The internal heap structures were not protected properly in memalign(). If multiple threads were concurrently allocating memory and one of them were requesting aligned memory via valloc,memalign or posix_memalign the internal heap data structures could be corrupted. Signed-off-by: Kjetil Oftedal <oftedal@gmail.com>
Diffstat (limited to 'libc/stdlib/malloc/memalign.c')
-rw-r--r--libc/stdlib/malloc/memalign.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/libc/stdlib/malloc/memalign.c b/libc/stdlib/malloc/memalign.c
index 6943279ac..665f20cfb 100644
--- a/libc/stdlib/malloc/memalign.c
+++ b/libc/stdlib/malloc/memalign.c
@@ -77,7 +77,9 @@ memalign (size_t alignment, size_t size)
init_size = addr - tot_addr;
}
+ __heap_lock (&__malloc_heap_lock);
__heap_free (heap, base, init_size);
+ __heap_unlock (&__malloc_heap_lock);
/* Remember that we've freed the initial part of MEM. */
base += init_size;
@@ -85,9 +87,11 @@ memalign (size_t alignment, size_t size)
/* Return the end part of MEM to the heap, unless it's too small. */
end_addr = addr + size;
- if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr)
+ if (end_addr + MALLOC_REALLOC_MIN_FREE_SIZE < tot_end_addr) {
+ __heap_lock (&__malloc_heap_lock);
__heap_free (heap, (void *)end_addr, tot_end_addr - end_addr);
- else
+ __heap_unlock (&__malloc_heap_lock);
+ } else
/* We didn't free the end, so include it in the size. */
end_addr = tot_end_addr;