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 removes it,
and then adds another command - this causes a segfault, because the
current undo position pointer of undostack is being moved to -1. This
patch removes a possibility of this segfault by clearing macros and
commands vector. If user has undo'd all commands (their position is at
-1), then it means that whenever eraseRedundantCmds() is called, a
macro or command has been pushed. If that happened, then we clear every
command that's currently in "undo" state. In this case everything to the
right is in undo state, so we simply need to clear everything.
  • Loading branch information
tetektoza committed Feb 5, 2024
1 parent 166d205 commit 5d42b78
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions source/undostack/undostack.cpp
Original file line number Diff line number Diff line change
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 5d42b78

Please sign in to comment.