From 1da70af724e16caa6ce175630f13434fcded512b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 1 Nov 2016 08:40:20 +0000 Subject: QMap: use std::less for defining an order between pointers Reinterpret_cast()ing a pointer to a suitably sized integer is not guaranteed to always give the same result for the same pointer (!). Therefore the resulting integers are not comparable in a meaningful way. std::less is supposed to be used to compare arbitrary pointers, so use it. (Hopefully and reasonably, under the hood std::less does exactly what we were doing, so this isn't BiC.) Change-Id: I9960b3d6e35657fe7a25b842054f5d338280e850 Reviewed-by: Thiago Macieira --- src/corelib/tools/qmap.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/corelib/tools/qmap.h') diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 96ce787446..3f4f034b4e 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -51,6 +51,7 @@ #include #include +#include #ifdef Q_COMPILER_INITIALIZER_LISTS #include @@ -61,11 +62,8 @@ QT_BEGIN_NAMESPACE /* QMap uses qMapLessThanKey() to compare keys. The default implementation uses operator<(). For pointer types, - qMapLessThanKey() casts the pointers to integers before it - compares them, because operator<() is undefined on pointers - that come from different memory blocks. (In practice, this - is only a problem when running a program such as - BoundsChecker.) + qMapLessThanKey() uses std::less (because operator<() on + pointers can be used only between pointers in the same array). */ template inline bool qMapLessThanKey(const Key &key1, const Key &key2) @@ -75,8 +73,7 @@ template inline bool qMapLessThanKey(const Key &key1, const Key &key template inline bool qMapLessThanKey(const Ptr *key1, const Ptr *key2) { - Q_STATIC_ASSERT(sizeof(quintptr) == sizeof(const Ptr *)); - return quintptr(key1) < quintptr(key2); + return std::less()(key1, key2); } struct QMapDataBase; -- cgit v1.2.3