Skip to content

Commit 9e71857

Browse files
committed
Handle overflow cases in split
1 parent 7945755 commit 9e71857

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

libc/src/__support/block.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,25 +427,24 @@ optional<Block *> Block::split(size_t new_inner_size,
427427
size_t usable_space_alignment) {
428428
LIBC_ASSERT(usable_space_alignment % alignof(max_align_t) == 0 &&
429429
"alignment must be a multiple of max_align_t");
430-
431430
if (used())
432431
return {};
433432

434-
size_t old_outer_size = outer_size();
435-
436433
// Compute the minimum outer size that produces a block of at least
437434
// `new_inner_size`.
438435
size_t min_outer_size = outer_size(cpp::max(new_inner_size, sizeof(prev_)));
439436

440437
uintptr_t start = reinterpret_cast<uintptr_t>(this);
441438
uintptr_t next_block_start =
442439
next_possible_block_start(start + min_outer_size, usable_space_alignment);
440+
if (next_block_start < start)
441+
return {};
443442
size_t new_outer_size = next_block_start - start;
444443
LIBC_ASSERT(new_outer_size % alignof(max_align_t) == 0 &&
445444
"new size must be aligned to max_align_t");
446445

447-
if (old_outer_size < new_outer_size ||
448-
old_outer_size - new_outer_size < sizeof(Block))
446+
if (outer_size() < new_outer_size ||
447+
outer_size() - new_outer_size < sizeof(Block))
449448
return {};
450449

451450
ByteSpan new_region = region().subspan(new_outer_size);

0 commit comments

Comments
 (0)