diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-07-27 11:06:59 +0300 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2019-07-29 12:45:08 +0000 |
commit | 93b845464fa7ac29c26e1835fd3a6cfac33d9a9d (patch) | |
tree | a6218a8cc69159c75e654d989dab2ef1c58ac08b /src/corelib/text | |
parent | adab531771a1ddce7df6652449b7977f340ebfc7 (diff) | |
download | qtbase-93b845464fa7ac29c26e1835fd3a6cfac33d9a9d.tar.gz |
QStringView: two fixes for newly-added toWCharArray()
Amends e89fbd8c3aa50a24e5fc02ab710ccca67fce98e2.
- While QString::data() never returns nullptr, QStringView::data()
may, which makes calling QStringView{}.toWCharArray() UB on Windows
(since memcpy's 2nd argument must never be nullptr, even if the size
is zero). Fix by protecting the memcpy call.
- QStringView, by design, does not use out-of-line member functions,
because calling these forces the QStringView object onto the stack.
Fix by making inline.
Also use the more efficient qToStringViewIgnoringNull(), as the result
does not depend on QString::isNull() (no characters are written
either way), and add a missing article to the function's docs.
Change-Id: I5d6b31361522812b0db8303b93c43d4b9ed11933
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qstring.h | 14 | ||||
-rw-r--r-- | src/corelib/text/qstringview.cpp | 14 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 2 |
3 files changed, 16 insertions, 14 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 08930f73b5..06ebb05fc7 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1122,7 +1122,19 @@ QT_WARNING_DISABLE_INTEL(111) // "statement is unreachable" inline int QString::toWCharArray(wchar_t *array) const { - return QStringView(*this).toWCharArray(array); + return qToStringViewIgnoringNull(*this).toWCharArray(array); +} + +int QStringView::toWCharArray(wchar_t *array) const +{ + if (sizeof(wchar_t) == sizeof(QChar)) { + if (auto src = data()) + memcpy(array, src, sizeof(QChar) * size()); + return size(); + } else { + return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), int(size()), + reinterpret_cast<uint *>(array)); + } } QT_WARNING_POP diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 8442bcdf1b..89cb4b32c0 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -867,11 +867,12 @@ QT_BEGIN_NAMESPACE */ /*! + \fn QStringView::toWCharArray(wchar_t *array) const \since 5.14 Transcribes this string into the given \a array. - Caller is responsible for ensuring \a array is large enough to hold the + The caller is responsible for ensuring \a array is large enough to hold the \c wchar_t encoding of this string (allocating the array with the same length as the string is always sufficient). The array is encoded in UTF-16 on platforms where \c wchar_t is 2 bytes wide (e.g. Windows); otherwise (Unix @@ -885,15 +886,4 @@ QT_BEGIN_NAMESPACE \sa QString::toWCharArray() */ -int QStringView::toWCharArray(wchar_t *array) const -{ - if (sizeof(wchar_t) == sizeof(QChar)) { - memcpy(array, data(), sizeof(QChar) * size()); - return size(); - } else { - return QString::toUcs4_helper(reinterpret_cast<const ushort *>(data()), int(size()), - reinterpret_cast<uint *>(array)); - } -} - QT_END_NAMESPACE diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index b84b2995b9..5a3acaa8c0 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -294,7 +294,7 @@ public: Q_REQUIRED_RESULT bool isRightToLeft() const noexcept { return QtPrivate::isRightToLeft(*this); } - Q_REQUIRED_RESULT Q_CORE_EXPORT int toWCharArray(wchar_t *array) const; + Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h // // STL compatibility API: |