From 49f9271360e0ef4e624212e442dfcd7765571d60 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 28 Sep 2023 08:15:05 +0200 Subject: [PATCH] QPersistentModelIndex: fix UB (op< on unrelated pointers) Pointers can only be legitimately compared with less-than (<) if they point into the same array (or one past the end). This is decidedly not the case for heap-allocated objects like QPersistentModelIndexPrivates, so doing it is UB. Fix by using std::less, which is guaranteed to be a total order, even for unrelated pointer values. Pick-to: 6.6 6.5 6.2 5.15 Change-Id: If04341b4b55784e7732782f3ae829f53b0ceab9c Reviewed-by: David Faure --- src/corelib/itemmodels/qabstractitemmodel.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index 31a88e8fec..a042dceca0 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -19,6 +19,8 @@ #include #include +#include + #include QT_BEGIN_NAMESPACE @@ -402,7 +404,7 @@ bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const if (d && other.d) return d->index < other.d->index; - return d < other.d; + return std::less<>{}(d, other.d); } /*!