From b460e5b14458e550dcee55c4f4bc5dc8014a2670 Mon Sep 17 00:00:00 2001 From: David Redondo Date: Thu, 25 Jun 2020 11:42:59 +0200 Subject: [PATCH] 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 --- kcms/keys/shortcutsmodel.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kcms/keys/shortcutsmodel.cpp b/kcms/keys/shortcutsmodel.cpp index 493f8f966..be113cc31 100644 --- a/kcms/keys/shortcutsmodel.cpp +++ b/kcms/keys/shortcutsmodel.cpp @@ -147,13 +147,14 @@ Component ShortcutsModel::loadComponent(const QList &info) void ShortcutsModel::save() { - for (auto& component : m_components) { - if (component.pendingDeletion) { - removeComponent(component); + for (auto it = m_components.rbegin(); it != m_components.rend(); ++it) { + if (it->pendingDeletion) { + removeComponent(*it); + continue; } - for (auto& shortcut : component.shortcuts) { + for (auto& shortcut : it->shortcuts) { 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); //operator int of QKeySequence QList keys(shortcut.activeShortcuts.cbegin(), shortcut.activeShortcuts.cend()); @@ -166,7 +167,7 @@ void ShortcutsModel::save() 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", - "Error while saving shortcut %1: %2", component.friendlyName, shortcut.friendlyName)); + "Error while saving shortcut %1: %2", it->friendlyName, shortcut.friendlyName)); } else { shortcut.initialShortcuts = shortcut.activeShortcuts; } -- GitLab