[Bugfix] EntityCollection.extend() skips gridstate resize when entity has existing gridstate #259

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

Summary

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

Root Cause

UIEntityCollection.cpp:687:

// 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))

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

large_grid.entities.extend([e1, e2])  # BUG: gridstate stays at 100
e1.update_visibility()  # heap overflow

Fix

Same as append() — 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::extend()` has the same `gridstate.size() == 0` guard as `.append()`, skipping the resize when an entity already has gridstate from a previous grid. ## Root Cause `UIEntityCollection.cpp:687`: ```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)) e1 = mcrfpy.Entity((5, 5)) e2 = mcrfpy.Entity((3, 3)) small_grid.entities.append(e1) small_grid.entities.append(e2) e1.update_visibility() # gridstate = 100 entries e2.update_visibility() # gridstate = 100 entries large_grid.entities.extend([e1, e2]) # BUG: gridstate stays at 100 e1.update_visibility() # heap overflow ``` ## Fix Same as append() — 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#259
No description provided.