From 3eb616f2bf05f61d857ac3c6464f85a14e48a710 Mon Sep 17 00:00:00 2001 From: Daan Date: Sun, 6 Nov 2022 14:51:06 -0800 Subject: [PATCH] fix bug in secure mode where adjustment would make the page size less than the blocksize on the first page of a segment --- src/page.c | 1 + src/segment.c | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/page.c b/src/page.c index 48ee1f56..f587fc9a 100644 --- a/src/page.c +++ b/src/page.c @@ -646,6 +646,7 @@ static void mi_page_init(mi_heap_t* heap, mi_page_t* page, size_t block_size, mi page->xblock_size = (block_size < MI_HUGE_BLOCK_SIZE ? (uint32_t)block_size : MI_HUGE_BLOCK_SIZE); mi_assert_internal(page_size / block_size < (1L<<16)); page->reserved = (uint16_t)(page_size / block_size); + mi_assert_internal(page->reserved > 0); #ifdef MI_ENCODE_FREELIST page->keys[0] = _mi_heap_random_next(heap); page->keys[1] = _mi_heap_random_next(heap); diff --git a/src/segment.c b/src/segment.c index 12c9e108..3c53db6f 100644 --- a/src/segment.c +++ b/src/segment.c @@ -403,12 +403,14 @@ uint8_t* _mi_segment_page_start(const mi_segment_t* segment, const mi_page_t* pa if (page->segment_idx == 0 && block_size > 0 && segment->page_kind <= MI_PAGE_MEDIUM) { // for small and medium objects, ensure the page start is aligned with the block size (PR#66 by kickunderscore) size_t adjust = block_size - ((uintptr_t)p % block_size); - if (adjust < block_size) { - p += adjust; - psize -= adjust; - if (pre_size != NULL) *pre_size = adjust; + if (psize - adjust >= block_size) { + if (adjust < block_size) { + p += adjust; + psize -= adjust; + if (pre_size != NULL) *pre_size = adjust; + } + mi_assert_internal((uintptr_t)p % block_size == 0); } - mi_assert_internal((uintptr_t)p % block_size == 0); } if (page_size != NULL) *page_size = psize;