From 35ffe8749e7af31b70e3e7b993dceab75aa95633 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 7 Dec 2023 18:34:02 -0800 Subject: [PATCH] QStringBuilder: use data_ptr().data() instead of constData() As the comment says, this allows us to skip the check for non-detached strings and for null ones. We know that the one we've just created isn't either, so long as len != 0. All QConcatenable::appendTo() calls do nothing if len == 0, including not calling memcpy() at all. Moreover, because it no longer references the QByteArray::_empty variable, the GCC -Warray-bounds warning should be gone too. Task-number: QTBUG-116763 Pick-to: 6.6 6.5 Change-Id: Ica7a43f6147b49c187ccfffd179eba2897ce2aff Reviewed-by: Marc Mutz Reviewed-by: Ahmad Samir (cherry picked from commit 06c724881c4227acf308e54ebd3db8235bb6b740) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qstringbuilder.h | 8 +++++--- src/gui/image/qimagereaderwriterhelpers.cpp | 3 --- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qstringbuilder.h b/src/corelib/text/qstringbuilder.h index 291988697fb..17160c65b61 100644 --- a/src/corelib/text/qstringbuilder.h +++ b/src/corelib/text/qstringbuilder.h @@ -104,9 +104,11 @@ private: const qsizetype len = QConcatenable< QStringBuilder >::size(*this); T s(len, Qt::Uninitialized); - // we abuse const_cast / constData here because we know we've just - // allocated the data and we're the only reference count - typename T::iterator d = const_cast(s.constData()); + // Using data_ptr() here (private API) so we can bypass the + // isDetached() and the replacement of a null pointer with _empty in + // both QString and QByteArray's data() and constData(). The result is + // the same if len != 0. + auto d = reinterpret_cast(s.data_ptr().data()); if constexpr (QConcatenable>::ExactSize) { QConcatenable>::appendTo(*this, d); diff --git a/src/gui/image/qimagereaderwriterhelpers.cpp b/src/gui/image/qimagereaderwriterhelpers.cpp index 9074f1cfe69..502b0f95f0d 100644 --- a/src/gui/image/qimagereaderwriterhelpers.cpp +++ b/src/gui/image/qimagereaderwriterhelpers.cpp @@ -99,8 +99,6 @@ QList supportedImageFormats(Capability cap) static constexpr QByteArrayView imagePrefix() noexcept { return "image/"; } -QT_WARNING_PUSH -QT_WARNING_DISABLE_GCC("-Warray-bounds") // QTBUG-116763 QList supportedMimeTypes(Capability cap) { QList mimeTypes; @@ -116,7 +114,6 @@ QList supportedMimeTypes(Capability cap) mimeTypes.erase(std::unique(mimeTypes.begin(), mimeTypes.end()), mimeTypes.end()); return mimeTypes; } -QT_WARNING_POP QList imageFormatsForMimeType(QByteArrayView mimeType, Capability cap) {