Skip to content

Commit 8b8057e

Browse files
committed
Issue #876: Fix integer overflow on slice_count
If the slice count doesn't fit into uint32_t, consider that the memory allocation failed. On s390x, allocating around 8,589,934,592 GiB with mmap() works thanks to overcommit on a machine with 8 GiB of memory: mmap(NULL, 0x8000000000400000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0)
1 parent f199b88 commit 8b8057e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/segment.c

+6
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,9 @@ static mi_segment_t* mi_segment_os_alloc( size_t required, size_t page_alignment
820820
const size_t extra = align_offset - info_size;
821821
// recalculate due to potential guard pages
822822
*psegment_slices = mi_segment_calculate_slices(required + extra, ppre_size, pinfo_slices);
823+
824+
// gh-876: mi_page_t.slice_count type is uint32_t
825+
if (*psegment_slices > (size_t)UINT32_MAX) return NULL;
823826
}
824827

825828
const size_t segment_size = (*psegment_slices) * MI_SEGMENT_SLICE_SIZE;
@@ -870,6 +873,9 @@ static mi_segment_t* mi_segment_alloc(size_t required, size_t page_alignment, mi
870873
size_t pre_size;
871874
size_t segment_slices = mi_segment_calculate_slices(required, &pre_size, &info_slices);
872875

876+
// gh-876: mi_page_t.slice_count type is uint32_t
877+
if (segment_slices > (size_t)UINT32_MAX) return NULL;
878+
873879
// Commit eagerly only if not the first N lazy segments (to reduce impact of many threads that allocate just a little)
874880
const bool eager_delay = (// !_mi_os_has_overcommit() && // never delay on overcommit systems
875881
_mi_current_thread_count() > 1 && // do not delay for the first N threads

0 commit comments

Comments
 (0)