diff --git a/lib/libimhex/source/providers/undo/stack.cpp b/lib/libimhex/source/providers/undo/stack.cpp index 1f45c2386..7deb8e5a5 100644 --- a/lib/libimhex/source/providers/undo/stack.cpp +++ b/lib/libimhex/source/providers/undo/stack.cpp @@ -80,6 +80,10 @@ namespace hex::prv::undo { for (u32 i = 0; i < count; i += 1) { i64 index = startIndex + i; + if (index < 0 || u64(index) >= m_undoStack.size()) { + break; + } + m_undoStack[index]->undo(m_provider); operation->addOperation(std::move(m_undoStack[index])); } diff --git a/plugins/ui/source/ui/hex_editor.cpp b/plugins/ui/source/ui/hex_editor.cpp index 514aadda0..2b692339b 100644 --- a/plugins/ui/source/ui/hex_editor.cpp +++ b/plugins/ui/source/ui/hex_editor.cpp @@ -356,25 +356,35 @@ namespace hex::ui { m_editingCellType = cellType; } } - } - else { + } else { + std::vector buffer = m_editingBytes; + if (m_mode == Mode::Insert) + std::ranges::fill(buffer, 0x00); + bool shouldExitEditingMode = true; - if (cellType == m_editingCellType && cellType == CellType::Hex) { - std::vector buffer = m_editingBytes; + if (cellType == m_editingCellType) { + switch (cellType) { + case CellType::Hex: { + if (m_dataVisualizerEndianness != std::endian::native) + std::ranges::reverse(buffer); - if (m_dataVisualizerEndianness != std::endian::native) - std::reverse(buffer.begin(), buffer.end()); + shouldExitEditingMode = m_currDataVisualizer->drawEditing(*m_editingAddress, buffer.data(), buffer.size(), m_upperCaseHex, m_enteredEditingMode); - shouldExitEditingMode = m_currDataVisualizer->drawEditing(*m_editingAddress, buffer.data(), buffer.size(), m_upperCaseHex, m_enteredEditingMode); - - if (m_dataVisualizerEndianness != std::endian::native) - std::reverse(buffer.begin(), buffer.end()); - - m_editingBytes = buffer; - } else if (cellType == m_editingCellType && cellType == CellType::ASCII) { - shouldExitEditingMode = asciiVisualizer.drawEditing(*m_editingAddress, m_editingBytes.data(), m_editingBytes.size(), m_upperCaseHex, m_enteredEditingMode); + if (m_dataVisualizerEndianness != std::endian::native) + std::ranges::reverse(buffer); + break; + } + case CellType::ASCII: { + shouldExitEditingMode = asciiVisualizer.drawEditing(*m_editingAddress, buffer.data(), buffer.size(), m_upperCaseHex, m_enteredEditingMode); + break; + } + default: + break; + } } + m_editingBytes = buffer; + if (ImGui::IsWindowFocused()) { ImGui::SetKeyboardFocusHere(-1); ImGui::SetNextFrameWantCaptureKeyboard(true); @@ -412,8 +422,10 @@ namespace hex::ui { if (m_mode == Mode::Insert) { std::memset(m_editingBytes.data(), 0x00, size); - m_provider->getUndoStack().groupOperations(2, "hex.builtin.undo_operation.insert"); m_provider->insert(nextEditingAddress, size); + + if (!shouldExitEditingMode) + m_provider->getUndoStack().groupOperations(2, "hex.builtin.undo_operation.insert"); } } } else {