summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qmap.h
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-17 17:38:41 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-19 01:45:25 +0200
commit8191a19df45007eec5259b0f865d3f823da75a50 (patch)
treee9c3475d377c2d296e38113411a26217a57f1496 /src/corelib/tools/qmap.h
parent2ba1d540e6175d06c21be86614704f9e56eb5d96 (diff)
QMultiMap: fix regression in find(Key, T)
1) Implementing the const version in terms of the non-const version exposes to accidental detaches. Avoid that. 2) The non-const version has to detach, just like find(Key), or doing a comparison like find(Key, T) != end() might report a wrong result. 3) Properly check if the value was found by checking find_if's return value (against its second parameter, the end of the iterated range). If the value was NOT found, then return the map's end() (again because clients of find() will check against end()). Change-Id: I03533e89f1e7a52ad888d159d78f38002765953c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'src/corelib/tools/qmap.h')
-rw-r--r--src/corelib/tools/qmap.h17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index f05e74b5ec..2780c46b7d 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -1249,22 +1249,29 @@ public:
iterator find(const Key &key, const T &value)
{
- if (!d)
- return iterator();
+ detach();
auto range = d->m.equal_range(key);
auto i = std::find_if(range.first, range.second,
MapData::valueIsEqualTo(value));
- return iterator(i);
+ if (i != range.second)
+ return iterator(i);
+ return iterator(d->m.end());
}
const_iterator find(const Key &key, const T &value) const
{
if (!d)
return const_iterator();
- // a bit evil, but effective
- return const_iterator(const_cast<QMultiMap *>(this)->find(key, value));
+
+ auto range = d->m.equal_range(key);
+ auto i = std::find_if(range.first, range.second,
+ MapData::valueIsEqualTo(value));
+
+ if (i != range.second)
+ return const_iterator(i);
+ return const_iterator(d->m.end());
}
const_iterator constFind(const Key &key, const T &value) const