Skip to content

Commit 2567e76

Browse files
Handle small pools in shape capacity calculation
When pool slot sizes can be smaller than sizeof(struct RBasic) (e.g. a 32-byte pool on 64-bit where RBasic is 16 bytes), the capacity calculation would underflow. Guard against this by setting capacity to 0 for pools too small to hold fields.
1 parent 9371042 commit 2567e76

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

shape.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,14 @@ static attr_index_t
477477
shape_grow_capa(attr_index_t current_capa)
478478
{
479479
const attr_index_t *capacities = rb_shape_tree.capacities;
480+
size_t heaps_count = rb_shape_tree.heaps_count;
480481

481482
// First try to use the next size that will be embeddable in a larger object slot.
482-
attr_index_t capa;
483-
while ((capa = *capacities)) {
483+
for (size_t i = 0; i < heaps_count; i++) {
484+
attr_index_t capa = capacities[i];
484485
if (capa > current_capa) {
485486
return capa;
486487
}
487-
capacities++;
488488
}
489489

490490
return (attr_index_t)rb_malloc_grow_capa(current_capa, sizeof(VALUE));
@@ -1543,8 +1543,14 @@ Init_default_shapes(void)
15431543
capacities[heaps_count] = 0;
15441544
size_t index;
15451545
for (index = 0; index < heaps_count; index++) {
1546-
capacities[index] = (heap_sizes[index] - sizeof(struct RBasic)) / sizeof(VALUE);
1546+
if (heap_sizes[index] > sizeof(struct RBasic)) {
1547+
capacities[index] = (heap_sizes[index] - sizeof(struct RBasic)) / sizeof(VALUE);
1548+
}
1549+
else {
1550+
capacities[index] = 0;
1551+
}
15471552
}
1553+
rb_shape_tree.heaps_count = heaps_count;
15481554
rb_shape_tree.capacities = capacities;
15491555

15501556
#ifdef HAVE_MMAP

shape.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ typedef struct {
115115
rb_shape_t *shape_list;
116116
rb_shape_t *root_shape;
117117
const attr_index_t *capacities;
118+
size_t heaps_count;
118119
rb_atomic_t next_shape_id;
119120

120121
redblack_node_t *shape_cache;

0 commit comments

Comments
 (0)