From 65ab38a8947a7ae89986f9539341bdfbdaf8b37c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 24 Jan 2021 15:28:18 +0100 Subject: [PATCH] sys/oneway-malloc: only allocate word-aligned chunks If an unevenly sized allocation is requested, the next buffer handed out would start at an unaligned address. Fix this by rounding up to the next word-sized allocation size. --- sys/oneway-malloc/oneway-malloc.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sys/oneway-malloc/oneway-malloc.c b/sys/oneway-malloc/oneway-malloc.c index fd3d13ca49..3ec46746e7 100644 --- a/sys/oneway-malloc/oneway-malloc.c +++ b/sys/oneway-malloc/oneway-malloc.c @@ -20,15 +20,23 @@ #include +#include "architecture.h" #include "malloc.h" #define ENABLE_DEBUG 0 #include "debug.h" +#define ARCHITECTURE_WORD_MASK (ARCHITECTURE_WORD_BYTES - 1) + extern void *sbrk(int incr); void __attribute__((weak)) *malloc(size_t size) { + /* ensure we always allocate word-aligned blocks */ + if (size & ARCHITECTURE_WORD_MASK) { + size += ARCHITECTURE_WORD_BYTES - (size & ARCHITECTURE_WORD_MASK); + } + if (size != 0) { void *ptr = sbrk(size);