[Bugfix] EntityCollection.insert() skips gridstate resize when entity has existing gridstate #260

Closed
opened 2026-03-07 23:19:20 +00:00 by john · 0 comments
Owner

Summary

UIEntityCollection::insert() has the same gridstate.size() == 0 guard as .append() and .extend(), skipping the resize when an entity already has gridstate from a previous grid.

Root Cause

UIEntityCollection.cpp:807:

// Initialize gridstate if needed
if (entity->data->gridstate.size() == 0 && self->grid) {
    entity->data->gridstate.resize(self->grid->grid_w * self->grid->grid_h);
    ...
}

Reproduction

import mcrfpy

small_grid = mcrfpy.Grid(grid_size=(10, 10))
large_grid = mcrfpy.Grid(grid_size=(50, 50))

entity = mcrfpy.Entity((5, 5))
small_grid.entities.append(entity)
entity.update_visibility()  # gridstate = 100 entries

large_grid.entities.insert(0, entity)  # BUG: gridstate stays at 100
entity.update_visibility()  # heap overflow

Fix

Same as append()/extend() — unconditionally resize gridstate to match the new grid dimensions.

Severity

Critical — heap buffer overflow, same class as the set_grid() bug fixed during 7DRL 2026.

## Summary `UIEntityCollection::insert()` has the same `gridstate.size() == 0` guard as `.append()` and `.extend()`, skipping the resize when an entity already has gridstate from a previous grid. ## Root Cause `UIEntityCollection.cpp:807`: ```cpp // Initialize gridstate if needed if (entity->data->gridstate.size() == 0 && self->grid) { entity->data->gridstate.resize(self->grid->grid_w * self->grid->grid_h); ... } ``` ## Reproduction ```python import mcrfpy small_grid = mcrfpy.Grid(grid_size=(10, 10)) large_grid = mcrfpy.Grid(grid_size=(50, 50)) entity = mcrfpy.Entity((5, 5)) small_grid.entities.append(entity) entity.update_visibility() # gridstate = 100 entries large_grid.entities.insert(0, entity) # BUG: gridstate stays at 100 entity.update_visibility() # heap overflow ``` ## Fix Same as append()/extend() — unconditionally resize gridstate to match the new grid dimensions. ## Severity **Critical** — heap buffer overflow, same class as the set_grid() bug fixed during 7DRL 2026.
john closed this issue 2026-03-14 06:25:16 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
john/McRogueFace#260
No description provided.