From b5eb553bf3bd6b9aad79241a65b9bebb40a848fd Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 1 Apr 2016 23:19:26 +0200 Subject: Fix UB (shift of negative number) in qHash(QModelIndex) Found by UBSan: itemmodels/qabstractitemmodel.h:426:28: runtime error: left shift of negative value -1 Fix by casting the lhs of the left-shift operator to uint before shifting. Since Qt assumes two's complement repre- sentation of signed integers, this should yield the same result as the old code, but without UBs. It is critically important that the result is identical to the old code (modulo the compiler exploiting the UB, which this patch aims to prevent even in future compilers), because the function is inline, and changing the hash value would mean changing the layout of a QHash between users compiled against the old and new libraries. Change-Id: I7b826a34fb78b02021e40c3f85fd11af398dbec4 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/itemmodels/qabstractitemmodel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/corelib/itemmodels') diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index 096e67c513..454134720d 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -423,7 +423,7 @@ inline Qt::ItemFlags QModelIndex::flags() const { return m ? m->flags(*this) : Qt::ItemFlags(); } inline uint qHash(const QModelIndex &index) Q_DECL_NOTHROW -{ return uint((index.row() << 4) + index.column() + index.internalId()); } +{ return uint((uint(index.row()) << 4) + index.column() + index.internalId()); } QT_END_NAMESPACE -- cgit v1.2.3