From 96775c7c1101c09576b20849dbf4ea1862a15347 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 30 Jan 2024 11:35:31 +0100 Subject: [PATCH] Deprecate QDataStream::readBytes(char *&, uint &) instead of removing it We cannot remove the overload using QT_REMOVED_SINCE, because a qint64 lvalue in the new overload will not bind to an uint& parameter, so the old code would not compile. Deprecate the old overload, and add a unit-test that makes sure that it still behaves correctly. This commit also introduces the new deprecation macros that are required to do the deprecation in Qt 6.11. Amends fd48ce0b73c74dafd5db27bc1f2752ef665df7ef Found in 6.7 API review Change-Id: I02893bfbe040df736f8e746384e0261a0f0041d3 Reviewed-by: Edward Welbourne Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira (cherry picked from commit cbc6ee0eb9ef42c7024fa527bb94ac89953709a6) --- src/corelib/compat/removed_api.cpp | 14 ------------ src/corelib/global/qtdeprecationmarkers.h | 14 ++++++++++++ src/corelib/serialization/qdatastream.cpp | 22 +++++++++++++++++++ src/corelib/serialization/qdatastream.h | 5 ++++- .../qdatastream/tst_qdatastream.cpp | 18 +++++++++++++++ 5 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 16c44f09a3c..4d6949d4026 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -706,20 +706,6 @@ bool QDateTime::precedes(const QDateTime &other) const #include "qdatastream.h" -QDataStream &QDataStream::readBytes(char *&s, uint &l) -{ - qint64 length = 0; - (void)readBytes(s, length); - if (length != qint64(uint(length))) { - setStatus(ReadCorruptData); // Cannot store length in l - delete[] s; - l = 0; - return *this; - } - l = uint(length); - return *this; -} - QDataStream &QDataStream::writeBytes(const char *s, uint len) { return writeBytes(s, qint64(len)); diff --git a/src/corelib/global/qtdeprecationmarkers.h b/src/corelib/global/qtdeprecationmarkers.h index a480a18eaec..6df5ebce6d2 100644 --- a/src/corelib/global/qtdeprecationmarkers.h +++ b/src/corelib/global/qtdeprecationmarkers.h @@ -211,6 +211,14 @@ QT_BEGIN_NAMESPACE # define QT_DEPRECATED_VERSION_6_10 #endif +#if QT_WARN_DEPRECATED_UP_TO >= QT_VERSION_CHECK(6, 11, 0) +# define QT_DEPRECATED_VERSION_X_6_11(text) QT_DEPRECATED_X(text) +# define QT_DEPRECATED_VERSION_6_11 QT_DEPRECATED +#else +# define QT_DEPRECATED_VERSION_X_6_11(text) +# define QT_DEPRECATED_VERSION_6_11 +#endif + #define QT_DEPRECATED_VERSION_X_5(minor, text) QT_DEPRECATED_VERSION_X_5_##minor(text) #define QT_DEPRECATED_VERSION_X(major, minor, text) QT_DEPRECATED_VERSION_X_##major##_##minor(text) @@ -313,6 +321,12 @@ QT_BEGIN_NAMESPACE # define QT_IF_DEPRECATED_SINCE_6_10(whenTrue, whenFalse) whenTrue #endif +#if QT_DEPRECATED_SINCE(6, 11) +# define QT_IF_DEPRECATED_SINCE_6_11(whenTrue, whenFalse) whenFalse +#else +# define QT_IF_DEPRECATED_SINCE_6_11(whenTrue, whenFalse) whenTrue +#endif + #ifdef __cplusplus // A tag to help mark stuff deprecated (cf. QStringViewLiteral) namespace QtPrivate { diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index 4239dc23977..23af4149048 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -1036,7 +1036,29 @@ QDataStream &QDataStream::operator>>(char32_t &c) return *this; } +#if QT_DEPRECATED_SINCE(6, 11) + +/* + \deprecated [6.11] Use an overload that takes qint64 length instead. +*/ +QDataStream &QDataStream::readBytes(char *&s, uint &l) +{ + qint64 length = 0; + (void)readBytes(s, length); + if (length != qint64(uint(length))) { + setStatus(SizeLimitExceeded); // Cannot store length in l + delete[] s; + l = 0; + return *this; + } + l = uint(length); + return *this; +} + +#endif // QT_DEPRECATED_SINCE(6, 11) + /*! + \since 6.7 Reads the buffer \a s from the stream and returns a reference to the stream. diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h index 55299c6eff3..2c9864b87ef 100644 --- a/src/corelib/serialization/qdatastream.h +++ b/src/corelib/serialization/qdatastream.h @@ -176,8 +176,11 @@ public: QDataStream &operator<<(char32_t c); QDataStream &operator<<(const volatile void *) = delete; -#if QT_CORE_REMOVED_SINCE(6, 7) +#if QT_DEPRECATED_SINCE(6, 11) + QT_DEPRECATED_VERSION_X_6_11("Use an overload that takes qint64 length.") QDataStream &readBytes(char *&, uint &len); +#endif +#if QT_CORE_REMOVED_SINCE(6, 7) QDataStream &writeBytes(const char *, uint len); int skipRawData(int len); int readRawData(char *, int len); diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index b934e54f432..35060c8749c 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -2840,6 +2840,24 @@ void tst_QDataStream::status_charptr_QByteArray() QCOMPARE(int(stream.status()), expectedStatus); delete [] buf; } +#if QT_DEPRECATED_SINCE(6, 11) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + { + // check that old overload still works as expected + QDataStream stream(&data, QIODevice::ReadOnly); + char *buf; + auto cleanup = qScopeGuard([&buf] { + delete [] buf; + }); + uint len; + stream.readBytes(buf, len); + + QCOMPARE(len, expectedString.size()); + QCOMPARE(QByteArray(buf, len), expectedString); + QCOMPARE(int(stream.status()), expectedStatus); + } +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 11) { QDataStream stream(&data, QIODevice::ReadOnly); QByteArray buf;