From 46872238ea9579cb2d4a7a348e0d91d426fe2b71 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 22 Feb 2023 07:53:29 +0100 Subject: [PATCH] QCryptographicHash: move SmallByteArray out of Private Rename it to QSmallByteArray, and make the maximum size a template argument. Initialize m_size to 0, to avoid a partially-formed default-constructed objects (default-constructed containers should always be in the empty state). As a drive-by, fix placement of some {'s. Don't move it into a header of its own, yet, as it lacks a lot of features expected of a generally-reusable class. Maybe one day. This is in preparation of re-using the class to hold the key in QMessageAuthenticationCode. Change-Id: Iee77e03e50afdf2ebc5889feeead344bef8ab3e8 Reviewed-by: Thiago Macieira (cherry picked from commit 29050fa65b7c755ec3238ff0cc238c626832bc8c) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qcryptographichash.cpp | 41 +++++++++++++----------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index e1dcf6fd293..2aa2b0947f3 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -117,6 +117,28 @@ static inline int SHA384_512AddLength(SHA512Context *context, unsigned int lengt QT_BEGIN_NAMESPACE +template +class QSmallByteArray +{ + std::array m_data; + static_assert(N <= std::numeric_limits::max()); + quint8 m_size = 0; +public: + // all SMFs are ok! + quint8 *data() noexcept { return m_data.data(); } + qsizetype size() const noexcept { return qsizetype{m_size}; } + bool isEmpty() const noexcept { return size() == 0; } + void clear() noexcept { m_size = 0; } + void resizeForOverwrite(qsizetype s) + { + Q_ASSERT(s >= 0); + Q_ASSERT(size_t(s) <= N); + m_size = std::uint8_t(s); + } + QByteArrayView toByteArrayView() const noexcept + { return QByteArrayView{m_data.data(), size()}; } +}; + static constexpr qsizetype MaxHashLength = 64; static constexpr int hashLengthInternal(QCryptographicHash::Algorithm method) noexcept @@ -269,26 +291,9 @@ public: void sha3Finish(int bitCount, Sha3Variant sha3Variant); #endif #endif - class SmallByteArray { - std::array m_data; - static_assert(MaxHashLength <= std::numeric_limits::max()); - quint8 m_size; - public: - quint8 *data() noexcept { return m_data.data(); } - qsizetype size() const noexcept { return qsizetype{m_size}; } - bool isEmpty() const noexcept { return size() == 0; } - void clear() noexcept { m_size = 0; } - void resizeForOverwrite(qsizetype s) { - Q_ASSERT(s >= 0); - Q_ASSERT(s <= MaxHashLength); - m_size = std::uint8_t(s); - } - QByteArrayView toByteArrayView() const noexcept - { return QByteArrayView{m_data.data(), size()}; } - }; // protects result in finalize() QBasicMutex finalizeMutex; - SmallByteArray result; + QSmallByteArray result; const QCryptographicHash::Algorithm method; };