diff options
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qhash.cpp | 30 | ||||
-rw-r--r-- | src/corelib/tools/qhash.h | 1 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 6119c945fe..ce7d4ad098 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -222,6 +222,31 @@ static void qt_initialize_qhash_seed() } } +/*! + \internal + + Private copy of the implementation of the Qt 4 qHash algorithm for strings, + to be used wherever the result is somehow stored or reused across multiple + Qt versions. The public qHash implementation can change at any time, + therefore one must not rely on the fact that it will always give the same + results. + + This function must *never* change its results. +*/ +uint qt_hash(const QString &key) +{ + const QChar *p = key.unicode(); + int n = key.size(); + uint h = 0; + + while (n--) { + h = (h << 4) + (*p++).unicode(); + h ^= (h & 0xf0000000) >> 23; + h &= 0x0fffffff; + } + return h; +} + /* The prime_deltas array is a table of selected prime values, even though it doesn't look like one. The primes we are using are 1, @@ -817,6 +842,11 @@ void QHashData::checkSanity() XOR'ed this with the day they were born to help produce unique hashes for people with the same name. + Note that the implementation of the qHash() overloads offered by Qt + may change at any time. You \b{must not} rely on the fact that qHash() + will give the same results (for the same inputs) across different Qt + versions. + \section2 Algorithmic complexity attacks All hash tables are vulnerable to a particular class of denial of service diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 42c33f6f78..533208da85 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -89,6 +89,7 @@ Q_CORE_EXPORT uint qHash(const QString &key, uint seed = 0); Q_CORE_EXPORT uint qHash(const QStringRef &key, uint seed = 0); Q_CORE_EXPORT uint qHash(const QBitArray &key, uint seed = 0); Q_CORE_EXPORT uint qHash(const QLatin1String &key, uint seed = 0); +Q_CORE_EXPORT uint qt_hash(const QString &key); #if defined(Q_CC_MSVC) #pragma warning( push ) |