From a77e9d671d3ff056a7989f31305c9351d89f111c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 21 Feb 2023 11:17:50 +0100 Subject: [PATCH] QCryptographicHash: Extract Method finalize() from resultView() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This brings the code in line with the sibling code in QMessageAuthenticationCode, which now has a simiar split between finalize() and finalizeUnchecked(). Pick-to: 6.5 Change-Id: I10701d59d56617ab32fae0df47371f0464e9cc77 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Qt CI Bot --- src/corelib/tools/qcryptographichash.cpp | 26 ++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 05a2d23f1da..3b1208be7ac 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -217,8 +217,9 @@ public: void reset() noexcept; void addData(QByteArrayView bytes) noexcept; + void finalize() noexcept; // when not called from the static hash() function, this function needs to be - // called with finalizeMutex held: + // called with finalizeMutex held (finalize() will do that): void finalizeUnchecked() noexcept; // END functions that need to be called with finalizeMutex held QByteArrayView resultView() const noexcept { return result.toByteArrayView(); } @@ -728,16 +729,25 @@ QByteArray QCryptographicHash::result() const QByteArrayView QCryptographicHash::resultView() const noexcept { // resultView() is a const function, so concurrent calls are allowed; protect: - { - const auto lock = qt_scoped_lock(d->finalizeMutex); - // check that no other thread already finalizeUnchecked()'ed before us: - if (d->result.isEmpty()) - d->finalizeUnchecked(); - } - // resultView() remains(!) valid even after we dropped the mutex + d->finalize(); + // resultView() remains(!) valid even after we dropped the mutex in finalize() return d->resultView(); } +/*! + \internal + + Calls finalizeUnchecked(), if needed, under finalizeMutex protection. +*/ +void QCryptographicHashPrivate::finalize() noexcept +{ + const auto lock = qt_scoped_lock(finalizeMutex); + // check that no other thread already finalizeUnchecked()'ed before us: + if (!result.isEmpty()) + return; + finalizeUnchecked(); +} + /*! \internal