Skip to content

Commit

Permalink
Undostack: Fix segfault when trying to fetch macro on -1 index
Browse files Browse the repository at this point in the history
Currently, if user adds a command to the undostack, and then undos it,
and then adds another command - this causes a segfault while fetching
macro ID from command on current undoPos. Current undo position pointer
of undostack is being moved to -1 if user undos every possible command
on the stack. Everytime user adds a new command, eraseRedundantCmds()
method is being called, because all commands that are in "undo" state
need to be cleared. So the solution to that is to simply clear every
single macro and command while erasing redundant commands, if the
current undo position is -1 - this basically means that every single
command/macro is in undo state, so we clear all of them.
  • Loading branch information
tetektoza authored and AJenbo committed Feb 6, 2024
1 parent 166d205 commit 3ca9b79
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions source/undostack/undostack.cpp
Expand Up @@ -184,6 +184,13 @@ void UndoStack::addMacro(UndoMacroFactory &macroFactory)
*/
void UndoStack::eraseRedundantCmds()
{
// If user has undo'd all the commands, then erase everything
if (m_undoPos < 0) {
m_cmds.clear();
m_macros.clear();
return;
}

// Erase any command that was set to obsolete
std::erase_if(m_cmds, [](const auto &cmd) { return cmd->isObsolete(); });

Expand Down

0 comments on commit 3ca9b79

Please sign in to comment.