Skip to content

Commit 1e77988

Browse files
committed
fix realloc from small sized allocations with above PAGE_SIZE alignment
Large allocations don't always have a size larger than the maximum slab size class because alignment larger than PAGE_SIZE is handled via large allocations. The general case in realloc was assuming small sizes imply slab allocations which isn't guaranteed. In practice, large alignments are extremely rare. Since realloc doesn't preserve alignment, combining both together doesn't happen in practice which is why this issue wasn't noticed. Reported-by: Stefan Rus <stefan@photonspark.com>
1 parent 1044b54 commit 1e77988

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

h_malloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,8 @@ EXPORT void *h_realloc(void *old, size_t size) {
15301530
old = untag_pointer(old);
15311531

15321532
size_t old_size;
1533-
if (old < get_slab_region_end() && old >= ro.slab_region_start) {
1533+
bool old_in_slab_region = old < get_slab_region_end() && old >= ro.slab_region_start;
1534+
if (old_in_slab_region) {
15341535
old_size = slab_usable_size(old);
15351536
if (size <= max_slab_size_class && get_size_info(size).size == old_size) {
15361537
return old_orig;
@@ -1647,7 +1648,7 @@ EXPORT void *h_realloc(void *old, size_t size) {
16471648
copy_size -= canary_size;
16481649
}
16491650
memcpy(new, old_orig, copy_size);
1650-
if (old_size <= max_slab_size_class) {
1651+
if (old_in_slab_region) {
16511652
deallocate_small(old, NULL);
16521653
} else {
16531654
deallocate_large(old, NULL);

0 commit comments

Comments
 (0)