From 93cb61e305f834a25ccbe0f2b64e0f8ebb72a168 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 2 Mar 2023 19:05:16 +0100 Subject: [PATCH] QMessageAuthenticationCode: port to QByteArrayView [1/3]: ctor/setKey() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The class is not used frequently enough to warrant the overhead of a Q_WEAK_ QByteArray overload to fix the SiC Type A for users that pass objects that implicitly convert to QByteArray. QCryptographicHash also doesn't have it. Also mark setKey() noexcept. Now that it takes a view instead of an owning container, it only calls other noexcept functions. ChangeLog will be on patch [3/3]. Task-number: QTBUG-111676 Task-number: QTBUG-111688 Change-Id: Ic2321d0d41ce8eb4d0390889f28b3fd4bd9af103 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/compat/removed_api.cpp | 11 +++++++++++ src/corelib/tools/qcryptographichash.cpp | 15 +++++++++++++-- src/corelib/tools/qmessageauthenticationcode.h | 10 +++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index d99ca421228..a04550350d2 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -486,6 +486,17 @@ void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QStr #if QT_CORE_REMOVED_SINCE(6, 6) +#include "qmessageauthenticationcode.h" + +QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, + const QByteArray &key) + : QMessageAuthenticationCode(method, qToByteArrayViewIgnoringNull(key)) {} + +void QMessageAuthenticationCode::setKey(const QByteArray &key) +{ + setKey(qToByteArrayViewIgnoringNull(key)); +} + #include "qstring.h" qsizetype QString::toUcs4_helper(const ushort *uc, qsizetype length, uint *out) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 3aedc067306..9ee2860a27e 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1245,9 +1245,18 @@ void QMessageAuthenticationCodePrivate::initMessageHash() noexcept /*! Constructs an object that can be used to create a cryptographic hash from data using method \a method and key \a key. + +//! [qba-to-qbav-6.6] + \note In Qt versions prior to 6.6, this function took its arguments as + QByteArray, not QByteArrayView. If you experience compile errors, it's + because your code is passing objects that are implicitly convertible to + QByteArray, but not QByteArrayView. Wrap the corresponding argument in + \c{QByteArray{~~~}} to make the cast explicit. This is backwards-compatible + with old Qt versions. +//! [qba-to-qbav-6.6] */ QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method, - const QByteArray &key) + QByteArrayView key) : d(new QMessageAuthenticationCodePrivate(method)) { d->setKey(key); @@ -1334,8 +1343,10 @@ void QMessageAuthenticationCode::reset() mac.emplace(method, key); use(*mac); \endcode + + \include qcryptographichash.cpp {qba-to-qbav-6.6} */ -void QMessageAuthenticationCode::setKey(const QByteArray &key) +void QMessageAuthenticationCode::setKey(QByteArrayView key) noexcept { d->result.clear(); d->messageHash.reset(); diff --git a/src/corelib/tools/qmessageauthenticationcode.h b/src/corelib/tools/qmessageauthenticationcode.h index 6be86823327..a753f7045fe 100644 --- a/src/corelib/tools/qmessageauthenticationcode.h +++ b/src/corelib/tools/qmessageauthenticationcode.h @@ -16,8 +16,13 @@ class QIODevice; class Q_CORE_EXPORT QMessageAuthenticationCode { public: +#if QT_CORE_REMOVED_SINCE(6, 6) explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method, - const QByteArray &key = QByteArray()); + const QByteArray &key); +#endif + explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method, + QByteArrayView key = {}); + QMessageAuthenticationCode(QMessageAuthenticationCode &&other) noexcept : d{std::exchange(other.d, nullptr)} {} ~QMessageAuthenticationCode(); @@ -28,7 +33,10 @@ public: void reset(); +#if QT_CORE_REMOVED_SINCE(6, 6) void setKey(const QByteArray &key); +#endif + void setKey(QByteArrayView key) noexcept; void addData(const char *data, qsizetype length); void addData(const QByteArray &data);