From a116b2ddfc9e91736b1ec4edda6500e9c8f5f301 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 6 Dec 2023 09:03:30 -0800 Subject: [PATCH] QByteArray: allow begin() to return nullptr, like QString That is, don't call QByteArray::data() because that one is still under QT5_NULL_STRINGS conditional. Instead, like QString, call d.data() directly, which is allowed to return a null pointer. This necessitated a fix to QStringBuilder's QConcatenable because it was iterating from &QByteArray::_empty to nullptr. [ChangeLog][Important Behavior Changes] Calling begin() or end() in a const QByteArray will return a null pointer if isNull() is true. This is the same behavior as QString. This is important for code attempting to iterate from data() to end() instead of begin() to end(). Change-Id: Ica7a43f6147b49c187ccfffd179e4cda75bd1031 Reviewed-by: Marc Mutz --- src/corelib/text/qbytearray.h | 2 +- src/corelib/text/qstringbuilder.h | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 06d310856b..ad4264036b 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -476,7 +476,7 @@ public: typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; iterator begin() { return data(); } - const_iterator begin() const noexcept { return data(); } + const_iterator begin() const noexcept { return d.data(); } const_iterator cbegin() const noexcept { return begin(); } const_iterator constBegin() const noexcept { return begin(); } iterator end() { return begin() + size(); } diff --git a/src/corelib/text/qstringbuilder.h b/src/corelib/text/qstringbuilder.h index 17160c65b6..dfe9863b74 100644 --- a/src/corelib/text/qstringbuilder.h +++ b/src/corelib/text/qstringbuilder.h @@ -374,10 +374,10 @@ template <> struct QConcatenable : private QAbstractConcatenable #endif static inline void appendTo(const QByteArray &ba, char *&out) { - const char *a = ba.constData(); - const char * const end = ba.end(); - while (a != end) - *out++ = *a++; + const qsizetype n = ba.size(); + if (n) + memcpy(out, ba.begin(), n); + out += n; } };