From 93fee70d0751651d3a538ff27566a1f8f88dfe82 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sun, 24 Nov 2019 00:51:30 +0000 Subject: [PATCH] use macro for string size calculation in qt_string_normalize() Signed-off-by: Ivailo Monev --- src/core/codecs/qicucodec.cpp | 18 +++++++----------- src/core/qcorecommon_p.h | 3 +++ src/core/tools/qchar.cpp | 9 ++++++--- src/core/tools/qstring.cpp | 5 +++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/core/codecs/qicucodec.cpp b/src/core/codecs/qicucodec.cpp index c56ea633e..fbc74a581 100644 --- a/src/core/codecs/qicucodec.cpp +++ b/src/core/codecs/qicucodec.cpp @@ -30,8 +30,8 @@ ****************************************************************************/ #include "qicucodec_p.h" - #include "qtextcodec_p.h" +#include "qcorecommon_p.h" #include "qdebug.h" #include @@ -419,17 +419,14 @@ UConverter *QIcuCodec::getConverter(QTextCodec::ConverterState *state) const return conv; } -// enough space to hold BOM, each char as surrogate pair and terminator -#define QMAXSTRLEN(X) 1 + (X * 2) + 2 - QString QIcuCodec::convertToUnicode(const char *src, int length, QTextCodec::ConverterState *state) const { UConverter *conv = getConverter(state); - QString string(QMAXSTRLEN(length), Qt::Uninitialized); - UChar *dest = reinterpret_cast(string.data()); + QString string(QMAXUSTRLEN(length), Qt::Uninitialized); UErrorCode error = U_ZERO_ERROR; - const int convresult = ucnv_toUChars(conv, dest, string.length(), src, length, &error); + const int convresult = ucnv_toUChars(conv, reinterpret_cast(string.data()), + string.length(), src, length, &error); if (Q_UNLIKELY(U_FAILURE(error))) { qWarning("QIcuCodec::convertToUnicode: failed %s", u_errorName(error)); if (state) { @@ -448,9 +445,9 @@ QString QIcuCodec::convertToUnicode(const char *src, int length, QTextCodec::Con if (!state) ucnv_close(conv); + return string; } -#undef QMAXSTRLEN QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QTextCodec::ConverterState *state) const { @@ -458,10 +455,9 @@ QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QText int maxbytes = UCNV_GET_MAX_BYTES_FOR_STRING(length, ucnv_getMaxCharSize(conv)); QByteArray string(maxbytes, Qt::Uninitialized); - const UChar *src = reinterpret_cast(unicode); - char *dest = reinterpret_cast(string.data()); UErrorCode error = U_ZERO_ERROR; - const int convresult = ucnv_fromUChars(conv, dest, string.length(), src, length, &error); + const int convresult = ucnv_fromUChars(conv, reinterpret_cast(string.data()), + string.length(), reinterpret_cast(unicode), length, &error); if (Q_UNLIKELY(U_FAILURE(error))) { qWarning("QIcuCodec::convertFromUnicode: failed %s", u_errorName(error)); if (state) { diff --git a/src/core/qcorecommon_p.h b/src/core/qcorecommon_p.h index a86b2285a..19c16bc6a 100644 --- a/src/core/qcorecommon_p.h +++ b/src/core/qcorecommon_p.h @@ -9,6 +9,9 @@ QT_BEGIN_NAMESPACE +// enough space to hold BOM, each char as surrogate pair and terminator +#define QMAXUSTRLEN(X) 1 + (X * 2) + 2 + #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && _POSIX_VERSION >= 200112L // There are two incompatible versions of strerror_r: // a) the XSI/POSIX.1 version, which returns an int, diff --git a/src/core/tools/qchar.cpp b/src/core/tools/qchar.cpp index f4e73dca3..bcda0ea3c 100644 --- a/src/core/tools/qchar.cpp +++ b/src/core/tools/qchar.cpp @@ -1148,8 +1148,10 @@ QString QChar::decomposition(const uint ucs4) return QString(); } - UChar buffer[4]; - const int32_t decresult = unorm2_getDecomposition(normalizer, ucs4, buffer, sizeof(buffer), &errorcode); + errorcode = U_ZERO_ERROR; + QString result(4, Qt::Uninitialized); + const int decresult = unorm2_getDecomposition(normalizer, ucs4, + reinterpret_cast(result.data()), result.size(), &errorcode); if (Q_UNLIKELY(decresult < 1)) { // no decomposition value return QString(); @@ -1160,7 +1162,8 @@ QString QChar::decomposition(const uint ucs4) return QString(); } - return QString::fromUtf16(reinterpret_cast(buffer), decresult); + result.resize(decresult); + return result; } /*! diff --git a/src/core/tools/qstring.cpp b/src/core/tools/qstring.cpp index 729ecc48d..8670dec0f 100644 --- a/src/core/tools/qstring.cpp +++ b/src/core/tools/qstring.cpp @@ -5789,9 +5789,10 @@ void qt_string_normalize(QString *data, QString::NormalizationForm mode, QChar:: return; } + errorcode = U_ZERO_ERROR; const int size = data->size() - from; - UChar buffer[size * 2]; - const int32_t decresult = unorm2_normalize(normalizer, reinterpret_cast(data->unicode() + from), + UChar buffer[QMAXUSTRLEN(size)]; + const int decresult = unorm2_normalize(normalizer, reinterpret_cast(data->unicode() + from), size, buffer, sizeof(buffer), &errorcode); if (Q_UNLIKELY(decresult < 1)) { // no normalization value -- 2.11.0