Commit b460e5b1 authored by David Redondo's avatar David Redondo 🏎 Committed by David Edmundson

Do not modify vector while iterating over it in range-based for loop

This is undefined behavior leading to wrong behavior or crashes. Instead iterate
backwards to not invalidate iterators that are still to be accessed.
BUG: 423224
BUG: 423441
FIXED-IN: 5.19.3
parent 6fa8a6b1
...@@ -147,13 +147,14 @@ Component ShortcutsModel::loadComponent(const QList<KGlobalShortcutInfo> &info) ...@@ -147,13 +147,14 @@ Component ShortcutsModel::loadComponent(const QList<KGlobalShortcutInfo> &info)
void ShortcutsModel::save() void ShortcutsModel::save()
{ {
for (auto& component : m_components) { for (auto it = m_components.rbegin(); it != m_components.rend(); ++it) {
if (component.pendingDeletion) { if (it->pendingDeletion) {
removeComponent(component); removeComponent(*it);
continue;
} }
for (auto& shortcut : component.shortcuts) { for (auto& shortcut : it->shortcuts) {
if (shortcut.initialShortcuts != shortcut.activeShortcuts) { if (shortcut.initialShortcuts != shortcut.activeShortcuts) {
const QStringList actionId = buildActionId(component.uniqueName, component.friendlyName, const QStringList actionId = buildActionId(it->uniqueName, it->friendlyName,
shortcut.uniqueName, shortcut.friendlyName); shortcut.uniqueName, shortcut.friendlyName);
//operator int of QKeySequence //operator int of QKeySequence
QList<int> keys(shortcut.activeShortcuts.cbegin(), shortcut.activeShortcuts.cend()); QList<int> keys(shortcut.activeShortcuts.cbegin(), shortcut.activeShortcuts.cend());
...@@ -166,7 +167,7 @@ void ShortcutsModel::save() ...@@ -166,7 +167,7 @@ void ShortcutsModel::save()
qCCritical(KCMKEYS) << reply.error().name() << reply.error().message(); qCCritical(KCMKEYS) << reply.error().name() << reply.error().message();
} }
emit errorOccured(i18nc("%1 is the name of the component, %2 is the action for which saving failed", emit errorOccured(i18nc("%1 is the name of the component, %2 is the action for which saving failed",
"Error while saving shortcut %1: %2", component.friendlyName, shortcut.friendlyName)); "Error while saving shortcut %1: %2", it->friendlyName, shortcut.friendlyName));
} else { } else {
shortcut.initialShortcuts = shortcut.activeShortcuts; shortcut.initialShortcuts = shortcut.activeShortcuts;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment