summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-17 17:31:44 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-19 01:45:19 +0200
commit2ba1d540e6175d06c21be86614704f9e56eb5d96 (patch)
tree126e5b1b72f8d161377fc99cf18708eceaf25f51 /src/corelib/tools
parent041f655c019788f375eec5db95d66e154b1e877b (diff)
QMultiMap: fix remove(Key, T) when key/value belong to the map
Just like any other container, it's legitimate for the user to pass key/values belonging to the same container. Q(Multi)Map::remove(Key) are already safe (either they call erase() directly on std::(multi)map, where it does the right thing, or they skip elements while detaching). However, QMultiMap::remove(Key, T) wasn't safe in this regard (the implementation is hand rolled), so take copies before start erasing. Change-Id: I87767d608b83216a6ff264fb6c8f145fdb5934f8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qmap.h12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index 65b3ba55db..f05e74b5ec 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -910,14 +910,20 @@ public:
// TODO: improve. Copy over only the elements not to be removed.
detach();
+ // key and value may belong to this map. As such, we need to copy
+ // them to ensure they stay valid througout the iteration below
+ // (which may destroy them)
+ const Key keyCopy = key;
+ const T valueCopy = value;
+
size_type result = 0;
const auto &keyCompare = d->m.key_comp();
- auto i = d->m.find(key);
+ auto i = d->m.find(keyCopy);
const auto e = d->m.end();
- while (i != e && !keyCompare(key, i->first)) {
- if (i->second == value) {
+ while (i != e && !keyCompare(keyCopy, i->first)) {
+ if (i->second == valueCopy) {
i = d->m.erase(i);
++result;
} else {