From 96dc4acb235f13a72bef7c719d005846fe1d9726 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 2 Mar 2023 19:45:04 +0100 Subject: [PATCH] QCryptographicHash: check addData() with null QByteArrayView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When this code was using QByteArray, whose data() is never nullptr, the strings presented to the underlying C APIs were always valid NTSs (nullptr is not a valid NTS). With QByteArrayView, or with QT5_NULL_STRINGS != 1, this is no longer the case. Check that all implementations are fine with that. Pick-to: 6.5 6.4 Change-Id: I78251288a4784440af4a2daf095aed7c53867287 Reviewed-by: Qt CI Bot Reviewed-by: MÃ¥rten Nordheim --- .../tst_qcryptographichash.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp index 2a0fd1a7c14..a7c22ffe292 100644 --- a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp +++ b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp @@ -31,6 +31,8 @@ private slots: void files(); void hashLength_data(); void hashLength(); + void addDataAcceptsNullByteArrayView_data() { hashLength_data(); } + void addDataAcceptsNullByteArrayView(); void move(); void swap(); // keep last @@ -414,6 +416,27 @@ void tst_QCryptographicHash::hashLength() QCOMPARE(QCryptographicHash::hashLength(algorithm), expectedSize); } +void tst_QCryptographicHash::addDataAcceptsNullByteArrayView() +{ + QFETCH(const QCryptographicHash::Algorithm, algorithm); + + if (!QCryptographicHash::supportsAlgorithm(algorithm)) + QSKIP("QCryptographicHash doesn't support this algorithm"); + + QCryptographicHash hash1(algorithm); + hash1.addData("meep"); + hash1.addData(QByteArrayView{}); // after other data + + QCryptographicHash hash2(algorithm); + hash2.addData(QByteArrayView{}); // before any other data + hash2.addData("meep"); + + const auto expected = QCryptographicHash::hash("meep", algorithm); + + QCOMPARE(hash1.resultView(), expected); + QCOMPARE(hash2.resultView(), expected); +} + void tst_QCryptographicHash::move() { QCryptographicHash hash1(QCryptographicHash::Sha1);