Fix crashes due to stack overflow when painting a large area in tile map #76548
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #76473.
This PR takes
alloca
out of the loop, reuses previous allocations whenever possible to reduce the chance of using up too much stack space.Cause
the crash described in linked issue is most likely (almost certain) caused by the stack overflow.
filling a large area of tiles at once will create a long list of operations for Undo/Redo to record, which gets processed by the for loop inside
UndoRedo::_process_operation_list
function, which usesalloca
inside. memory obtained fromalloca
gets freed after you leave the function scope.given long enough list and right condition (which can be achieved by using a CTRL+SHIFT fill on tile map),
alloca
can exhaust the stack and cause a stack overflow, resulting in crash.Valgrind report
each call allocates around 40 bytes (8 bytes for pointer * 5 arguments), resulting in roughly 6.6 MB before the crash