From 4c0f8dd5c0bac4e739fdd32bc4a5a0860bf7be54 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 6 May 2026 10:13:56 -0700 Subject: [PATCH] fix(history): bounds-check chunkIndex in captureChunk/restoreChunk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ADTTerrain.chunks is std::array — out-of-range indexing is undefined behaviour. Reject indices outside [0, 255] and return empty / no-op rather than crashing on a stale undo record from a future-version terrain layout. --- tools/editor/editor_history.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/editor/editor_history.cpp b/tools/editor/editor_history.cpp index 18636f74..1ec88781 100644 --- a/tools/editor/editor_history.cpp +++ b/tools/editor/editor_history.cpp @@ -5,6 +5,9 @@ namespace editor { ChunkSnapshot EditorHistory::captureChunk(const pipeline::ADTTerrain& terrain, int idx) { ChunkSnapshot snap; + // ADTTerrain.chunks is std::array; out-of-range + // would be undefined behaviour. Return an empty snapshot instead. + if (idx < 0 || idx >= 256) return snap; snap.chunkIndex = idx; snap.heights = terrain.chunks[idx].heightMap.heights; snap.alphaMap = terrain.chunks[idx].alphaMap; @@ -13,6 +16,7 @@ ChunkSnapshot EditorHistory::captureChunk(const pipeline::ADTTerrain& terrain, i } void EditorHistory::restoreChunk(pipeline::ADTTerrain& terrain, const ChunkSnapshot& snap) { + if (snap.chunkIndex < 0 || snap.chunkIndex >= 256) return; terrain.chunks[snap.chunkIndex].heightMap.heights = snap.heights; terrain.chunks[snap.chunkIndex].alphaMap = snap.alphaMap; terrain.chunks[snap.chunkIndex].layers = snap.layers;