From 4a3d0a5a5d829c05868a34658eb45731dbb5112b Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Thu, 24 May 2018 23:53:15 -0400 Subject: Fix issue with malloc_extend_top - when calculating a correction to align next brk to page boundary, ensure that the correction is less than a page size - if allocating the correction fails, ensure that the top size is set to brk + sbrk_size (minus any front alignment made) Signed-off-by: Jeff Johnston --- newlib/libc/stdlib/mallocr.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/newlib/libc/stdlib/mallocr.c b/newlib/libc/stdlib/mallocr.c index ecc445f3d..26d1c89cc 100644 --- a/newlib/libc/stdlib/mallocr.c +++ b/newlib/libc/stdlib/mallocr.c @@ -2198,13 +2198,18 @@ static void malloc_extend_top(RARG nb) RDECL INTERNAL_SIZE_T nb; /* Guarantee the next brk will be at a page boundary */ correction += pagesz - ((POINTER_UINT)(brk + sbrk_size) & (pagesz - 1)); + /* To guarantee page boundary, correction should be less than pagesz */ + correction &= (pagesz - 1); + /* Allocate correction */ new_brk = (char*)(MORECORE (correction)); if (new_brk == (char*)(MORECORE_FAILURE)) { correction = 0; correction_failed = 1; - new_brk = brk; + new_brk = brk + sbrk_size; + if (front_misalign > 0) + new_brk -= (MALLOC_ALIGNMENT) - front_misalign; } sbrked_mem += correction; -- cgit v1.2.3