Skip to content

Commit 5eec38a

Browse files
committed
Fix Grid::remove_object to validate object ID before clearing grid cell
Prevents inconsistent state where grid cell is cleared but object may still exist in memory if obj.id >= objects.size(). Now returns false instead of true when the ID is invalid.
1 parent b2070f0 commit 5eec38a

File tree

1 file changed

+6
-3
lines changed
  • packages/mettagrid/cpp/include/mettagrid/core

1 file changed

+6
-3
lines changed

packages/mettagrid/cpp/include/mettagrid/core/grid.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,16 @@ class Grid {
104104
return false; // Object not at expected location
105105
}
106106

107+
// Validate object ID first
108+
if (obj.id >= objects.size()) {
109+
return false;
110+
}
111+
107112
// Clear the grid cell
108113
grid[obj.location.r][obj.location.c] = nullptr;
109114

110115
// Release the object (unique_ptr becomes null but slot remains)
111-
if (obj.id < objects.size()) {
112-
objects[obj.id].reset();
113-
}
116+
objects[obj.id].reset();
114117
return true;
115118
}
116119

0 commit comments

Comments
 (0)