From 8a1eb24de8ef063fc5d0c3f10a7324d95bbc8709 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Wed, 8 Mar 2023 13:06:21 +0200 Subject: [PATCH] Misc: Fix qsizetype-related narrowing coversions Task-number: QTBUG-102461 Change-Id: I96757abc50fc45756bc1271a970f819a48021663 Reviewed-by: Thiago Macieira --- src/corelib/global/qlibraryinfo.cpp | 6 +++--- src/corelib/global/qsysinfo.cpp | 7 +++---- src/corelib/io/qfilesystemengine_unix.cpp | 4 ++-- src/corelib/io/qloggingregistry.cpp | 4 ++-- src/corelib/io/qprocess.cpp | 2 +- src/corelib/io/qresource.cpp | 8 ++++---- src/corelib/io/qsettings.cpp | 2 +- src/corelib/mimetypes/qmimetype.cpp | 6 +++--- src/corelib/text/qstring.cpp | 2 +- src/corelib/text/qstringconverter.cpp | 12 ++++++------ 10 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/corelib/global/qlibraryinfo.cpp b/src/corelib/global/qlibraryinfo.cpp index 715bda488c..68db820f01 100644 --- a/src/corelib/global/qlibraryinfo.cpp +++ b/src/corelib/global/qlibraryinfo.cpp @@ -542,8 +542,8 @@ QString QLibraryInfoPrivate::path(QLibraryInfo::LibraryPath p, UsageMode usageMo ret = v.toString(); } - int startIndex = 0; - forever { + qsizetype startIndex = 0; + while (true) { startIndex = ret.indexOf(u'$', startIndex); if (startIndex < 0) break; @@ -553,7 +553,7 @@ QString QLibraryInfoPrivate::path(QLibraryInfo::LibraryPath p, UsageMode usageMo startIndex++; continue; } - int endIndex = ret.indexOf(u')', startIndex + 2); + qsizetype endIndex = ret.indexOf(u')', startIndex + 2); if (endIndex < 0) break; auto envVarName = QStringView{ret}.mid(startIndex + 2, endIndex - startIndex - 2); diff --git a/src/corelib/global/qsysinfo.cpp b/src/corelib/global/qsysinfo.cpp index dace55b6ce..20919d0865 100644 --- a/src/corelib/global/qsysinfo.cpp +++ b/src/corelib/global/qsysinfo.cpp @@ -352,8 +352,7 @@ static QByteArray getEtcFileFirstLine(const char *fileName) return QByteArray(); const char *ptr = buffer.constData(); - int eol = buffer.indexOf("\n"); - return QByteArray(ptr, eol).trimmed(); + return QByteArray(ptr, buffer.indexOf("\n")).trimmed(); } static bool readEtcRedHatRelease(QUnixOSVersion &v) @@ -368,9 +367,9 @@ static bool readEtcRedHatRelease(QUnixOSVersion &v) v.prettyName = QString::fromLatin1(line); const char keyword[] = "release "; - int releaseIndex = line.indexOf(keyword); + const qsizetype releaseIndex = line.indexOf(keyword); v.productType = QString::fromLatin1(line.mid(0, releaseIndex)).remove(u' '); - int spaceIndex = line.indexOf(' ', releaseIndex + strlen(keyword)); + const qsizetype spaceIndex = line.indexOf(' ', releaseIndex + strlen(keyword)); v.productVersion = QString::fromLatin1(line.mid(releaseIndex + strlen(keyword), spaceIndex > -1 ? spaceIndex - releaseIndex - int(strlen(keyword)) : -1)); return true; diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 9909afaba7..fba8667290 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -1104,7 +1104,7 @@ static bool createDirectoryWithParents(const QByteArray &nativeName, mode_t mode return false; // mkdir failed because the parent dir doesn't exist, so try to create it - int slash = nativeName.lastIndexOf('/'); + qsizetype slash = nativeName.lastIndexOf('/'); if (slash < 1) return false; @@ -1147,7 +1147,7 @@ bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool remo if (removeEmptyParents) { QString dirName = QDir::cleanPath(entry.filePath()); - for (int oldslash = 0, slash=dirName.size(); slash > 0; oldslash = slash) { + for (qsizetype oldslash = 0, slash=dirName.size(); slash > 0; oldslash = slash) { const QByteArray chunk = QFile::encodeName(dirName.left(slash)); QT_STATBUF st; if (QT_STAT(chunk.constData(), &st) != -1) { diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp index 9805722213..3c835cce5a 100644 --- a/src/corelib/io/qloggingregistry.cpp +++ b/src/corelib/io/qloggingregistry.cpp @@ -67,7 +67,7 @@ int QLoggingRule::pass(QLatin1StringView cat, QtMsgType msgType) const return 0; } - const int idx = cat.indexOf(category); + const qsizetype idx = cat.indexOf(category); if (idx >= 0) { if (flags == MidFilter) { // matches somewhere @@ -194,7 +194,7 @@ void QLoggingSettingsParser::parseNextLine(QStringView line) } if (m_inRulesSection) { - int equalPos = line.indexOf(u'='); + const qsizetype equalPos = line.indexOf(u'='); if (equalPos != -1) { if (line.lastIndexOf(u'=') == equalPos) { const auto key = line.left(equalPos).trimmed(); diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 27994591e6..49ba04068a 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -72,7 +72,7 @@ QProcessEnvironment QProcessEnvironmentPrivate::fromList(const QStringList &list QStringList::ConstIterator it = list.constBegin(), end = list.constEnd(); for ( ; it != end; ++it) { - int pos = it->indexOf(u'=', 1); + const qsizetype pos = it->indexOf(u'=', 1); if (pos < 1) continue; diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp index 3fd5a55637..a0759bd1b5 100644 --- a/src/corelib/io/qresource.cpp +++ b/src/corelib/io/qresource.cpp @@ -82,7 +82,7 @@ public: inline QStringView next() { - int start = m_pos; + const qsizetype start = m_pos; while (m_pos < m_len && m_data[m_pos] != m_splitChar) ++m_pos; return QStringView(m_data + start, m_pos - start); @@ -1478,14 +1478,14 @@ QString QResourceFileEngine::fileName(FileName file) const { Q_D(const QResourceFileEngine); if (file == BaseName) { - int slash = d->resource.fileName().lastIndexOf(u'/'); + const qsizetype slash = d->resource.fileName().lastIndexOf(u'/'); if (slash == -1) return d->resource.fileName(); return d->resource.fileName().mid(slash + 1); } else if (file == PathName || file == AbsolutePathName) { const QString path = (file == AbsolutePathName) ? d->resource.absoluteFilePath() : d->resource.fileName(); - const int slash = path.lastIndexOf(u'/'); + const qsizetype slash = path.lastIndexOf(u'/'); if (slash == -1) return ":"_L1; else if (slash <= 1) @@ -1495,7 +1495,7 @@ QString QResourceFileEngine::fileName(FileName file) const } else if (file == CanonicalName || file == CanonicalPathName) { const QString absoluteFilePath = d->resource.absoluteFilePath(); if (file == CanonicalPathName) { - const int slash = absoluteFilePath.lastIndexOf(u'/'); + const qsizetype slash = absoluteFilePath.lastIndexOf(u'/'); if (slash != -1) return absoluteFilePath.left(slash); } diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 184042593f..3f33239334 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -558,7 +558,7 @@ bool QSettingsPrivate::iniUnescapedKey(QByteArrayView key, QString &result) } int numDigits = 2; - int firstDigitPos = i + 1; + qsizetype firstDigitPos = i + 1; ch = decoded.at(i + 1).unicode(); if (ch == 'U') { diff --git a/src/corelib/mimetypes/qmimetype.cpp b/src/corelib/mimetypes/qmimetype.cpp index 24780ffe27..cf153fd8d5 100644 --- a/src/corelib/mimetypes/qmimetype.cpp +++ b/src/corelib/mimetypes/qmimetype.cpp @@ -230,7 +230,7 @@ QString QMimeType::comment() const const QString comm = d->localeComments.value(lang); if (!comm.isEmpty()) return comm; - const int pos = lang.indexOf(u'_'); + const qsizetype pos = lang.indexOf(u'_'); if (pos != -1) { // "pt_BR" not found? try just "pt" const QString shortLang = lang.left(pos); @@ -269,7 +269,7 @@ QString QMimeType::genericIconName() const // (i.e. "video-x-generic" in the previous example). const QString group = name(); QStringView groupRef(group); - const int slashindex = groupRef.indexOf(u'/'); + const qsizetype slashindex = groupRef.indexOf(u'/'); if (slashindex != -1) groupRef = groupRef.left(slashindex); return groupRef + "-x-generic"_L1; @@ -279,7 +279,7 @@ QString QMimeType::genericIconName() const static QString make_default_icon_name_from_mimetype_name(QString iconName) { - const int slashindex = iconName.indexOf(u'/'); + const qsizetype slashindex = iconName.indexOf(u'/'); if (slashindex != -1) iconName[slashindex] = u'-'; return iconName; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 51b414634d..2d387d1e05 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -1252,7 +1252,7 @@ Q_NEVER_INLINE static int ucstricmp8(const char *utf8, const char *utf8end, cons char32_t uc1 = 0; char32_t *output = &uc1; uchar b = *src1++; - int res = QUtf8Functions::fromUtf8(b, output, src1, end1); + const qsizetype res = QUtf8Functions::fromUtf8(b, output, src1, end1); if (res < 0) { // decoding error uc1 = QChar::ReplacementCharacter; diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 97072b427a..90f277ba61 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -651,7 +651,7 @@ char16_t *QUtf8::convertToUnicode(char16_t *dst, QByteArrayView in) noexcept do { uchar b = *src++; - int res = QUtf8Functions::fromUtf8(b, dst, src, end); + const qsizetype res = QUtf8Functions::fromUtf8(b, dst, src, end); if (res < 0) { // decoding error *dst++ = QChar::ReplacementCharacter; @@ -694,7 +694,7 @@ char16_t *QUtf8::convertToUnicode(char16_t *dst, QByteArrayView in, QStringConve if (state->flags & QStringConverter::Flag::ConvertInvalidToNull) replacement = QChar::Null; - int res; + qsizetype res; uchar ch = 0; const uchar *src = reinterpret_cast(in.data()); @@ -810,7 +810,7 @@ QUtf8::ValidUtf8Result QUtf8::isValidUtf8(QByteArrayView in) isValidAscii = false; QUtf8NoOutputTraits::NoOutput output; - int res = QUtf8Functions::fromUtf8(b, output, src, end); + const qsizetype res = QUtf8Functions::fromUtf8(b, output, src, end); if (res < 0) { // decoding error return { false, false }; @@ -837,7 +837,7 @@ int QUtf8::compareUtf8(QByteArrayView utf8, QStringView utf16, Qt::CaseSensitivi if (uc1 >= 0x80) { char32_t *output = &uc1; - int res = QUtf8Functions::fromUtf8(uc1, output, src1, end1); + qsizetype res = QUtf8Functions::fromUtf8(uc1, output, src1, end1); if (res < 0) { // decoding error uc1 = QChar::ReplacementCharacter; @@ -872,7 +872,7 @@ int QUtf8::compareUtf8(QByteArrayView utf8, QLatin1StringView s, Qt::CaseSensiti while (src1 < end1 && src2 < end2) { uchar b = *src1++; char32_t *output = &uc1; - int res = QUtf8Functions::fromUtf8(b, output, src1, end1); + const qsizetype res = QUtf8Functions::fromUtf8(b, output, src1, end1); if (res < 0) { // decoding error uc1 = QChar::ReplacementCharacter; @@ -912,7 +912,7 @@ int QUtf8::compareUtf8(QByteArrayView lhs, QByteArrayView rhs, Qt::CaseSensitivi while (src1 < end1 && src2 < end2) { uchar b = *src1++; char32_t *output = &uc1; - int res = QUtf8Functions::fromUtf8(b, output, src1, end1); + qsizetype res = QUtf8Functions::fromUtf8(b, output, src1, end1); if (res < 0) { // decoding error uc1 = QChar::ReplacementCharacter;