From 8397a44bedf542b53284674c87268819f4911d31 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 22 Feb 2012 16:17:30 +0100 Subject: QByteArray: deprecate QT_NO_CAST_FROM_BYTEARRAY-protected operators The QByteArray::operator const {char,void}*() implicit conversions are a source of subtle bugs, so they right- fully can be disabled with QT_NO_CAST_FROM_BYTEARRAY. const char *d = qstring.toLatin1(); // implicit conversion while ( d ) // oops: d points to freed memory // ... But almost no-one ever enabled this macros in the wild and many were bitten by these implicit conversions, so this patch deprecates them. I would have liked to remove them completely, but there are just too many occurrences even in Qt itself to hope to find all conditionally-compiled code that uses these. Also fixes all code that needs to compile under QT_NO_DEPRECATED (in qmake/, src/tools/). I984706452db7d0841620a0f64e179906123f3849 separately deals with the bulk of changes in src/ and examples/. Depends on I5ea1ad3c96d9e64167be53c0c418c7b7dba51f68. Change-Id: I8d47e6c293c80f61c6288c9f8d42fda41afe2267 Reviewed-by: David Faure Reviewed-by: Lars Knoll --- src/corelib/codecs/qtextcodec.cpp | 6 +++--- src/corelib/io/qfilesystemengine_unix.cpp | 18 +++++++++--------- src/corelib/io/qsettings.cpp | 6 +++--- src/corelib/kernel/qvariant.cpp | 2 +- src/corelib/tools/qbytearray.cpp | 6 ++++-- src/corelib/tools/qbytearray.h | 12 ++++-------- src/corelib/tools/qlocale_unix.cpp | 2 +- src/corelib/tools/qstring.cpp | 2 +- src/tools/rcc/rcc.cpp | 2 +- src/tools/uic/cpp/cppwriteicondata.cpp | 4 ++-- src/xml/dom/qdom.cpp | 8 ++++---- src/xml/sax/qxml.cpp | 6 +++--- 12 files changed, 36 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp index 82e5c9a1c3..e49ebfb844 100644 --- a/src/corelib/codecs/qtextcodec.cpp +++ b/src/corelib/codecs/qtextcodec.cpp @@ -111,7 +111,7 @@ static bool qisalnum(register char c) static bool nameMatch(const QByteArray &name, const QByteArray &test) { // if they're the same, return a perfect score - if (qstricmp(name, test) == 0) + if (qstricmp(name.constData(), test.constData()) == 0) return true; const char *n = name.constData(); @@ -510,7 +510,7 @@ static QTextCodec * ru_RU_hack(const char * i) { koi8r, latin5, i); } #if !defined(QT_NO_SETLOCALE) - setlocale(LC_CTYPE, origlocale); + setlocale(LC_CTYPE, origlocale.constData()); #endif return ru_RU_codec; @@ -648,7 +648,7 @@ static void setupLocaleMapper() else if (try_locale_list(pt_154locales, lang)) localeMapper = QTextCodec::codecForName("PT 154"); else if (try_locale_list(probably_koi8_rlocales, lang)) - localeMapper = ru_RU_hack(lang); + localeMapper = ru_RU_hack(lang.constData()); } } diff --git a/src/corelib/io/qfilesystemengine_unix.cpp b/src/corelib/io/qfilesystemengine_unix.cpp index 5e466e480d..e8ff6107ce 100644 --- a/src/corelib/io/qfilesystemengine_unix.cpp +++ b/src/corelib/io/qfilesystemengine_unix.cpp @@ -476,12 +476,12 @@ bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool crea slash = dirName.length(); } if (slash) { - QByteArray chunk = QFile::encodeName(dirName.left(slash)); + const QByteArray chunk = QFile::encodeName(dirName.left(slash)); QT_STATBUF st; - if (QT_STAT(chunk, &st) != -1) { + if (QT_STAT(chunk.constData(), &st) != -1) { if ((st.st_mode & S_IFMT) != S_IFDIR) return false; - } else if (QT_MKDIR(chunk, 0777) != 0) { + } else if (QT_MKDIR(chunk.constData(), 0777) != 0) { return false; } } @@ -492,7 +492,7 @@ bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool crea if (dirName.endsWith(QLatin1Char('/'))) dirName.chop(1); #endif - return (QT_MKDIR(QFile::encodeName(dirName), 0777) == 0); + return (QT_MKDIR(QFile::encodeName(dirName).constData(), 0777) == 0); } //static @@ -501,12 +501,12 @@ bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool remo if (removeEmptyParents) { QString dirName = QDir::cleanPath(entry.filePath()); for (int oldslash = 0, slash=dirName.length(); slash > 0; oldslash = slash) { - QByteArray chunk = QFile::encodeName(dirName.left(slash)); + const QByteArray chunk = QFile::encodeName(dirName.left(slash)); QT_STATBUF st; - if (QT_STAT(chunk, &st) != -1) { + if (QT_STAT(chunk.constData(), &st) != -1) { if ((st.st_mode & S_IFMT) != S_IFDIR) return false; - if (::rmdir(chunk) != 0) + if (::rmdir(chunk.constData()) != 0) return oldslash != 0; } else { return false; @@ -515,7 +515,7 @@ bool QFileSystemEngine::removeDirectory(const QFileSystemEntry &entry, bool remo } return true; } - return rmdir(QFile::encodeName(entry.filePath())) == 0; + return rmdir(QFile::encodeName(entry.filePath()).constData()) == 0; } //static @@ -623,7 +623,7 @@ QString QFileSystemEngine::tempPath() bool QFileSystemEngine::setCurrentPath(const QFileSystemEntry &path) { int r; - r = QT_CHDIR(path.nativeFilePath()); + r = QT_CHDIR(path.nativeFilePath().constData()); return r >= 0; } diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 2021c42c4d..f743c592bd 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -1752,10 +1752,10 @@ bool QConfFileSettingsPrivate::readIniFile(const QByteArray &data, iniSection = iniSection.trimmed(); - if (qstricmp(iniSection, "general") == 0) { + if (qstricmp(iniSection.constData(), "general") == 0) { currentSection.clear(); } else { - if (qstricmp(iniSection, "%general") == 0) { + if (qstricmp(iniSection.constData(), "%general") == 0) { currentSection = QLatin1String(iniSection.constData() + 1); } else { currentSection.clear(); @@ -1912,7 +1912,7 @@ bool QConfFileSettingsPrivate::writeIniFile(QIODevice &device, const ParsedSetti if (realSection.isEmpty()) { realSection = "[General]"; - } else if (qstricmp(realSection, "general") == 0) { + } else if (qstricmp(realSection.constData(), "general") == 0) { realSection = "[%General]"; } else { realSection.prepend('['); diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 4e7fd94141..2f67ae9287 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -1698,7 +1698,7 @@ void QVariant::load(QDataStream &s) if (typeId == QVariant::UserType) { QByteArray name; s >> name; - typeId = QMetaType::type(name); + typeId = QMetaType::type(name.constData()); if (!typeId) { s.setStatus(QDataStream::ReadCorruptData); return; diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index c74c61999d..1d37f578b8 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -343,7 +343,7 @@ int qstrcmp(const QByteArray &str1, const QByteArray &str2) { int l1 = str1.length(); int l2 = str2.length(); - int ret = memcmp(str1, str2, qMin(l1, l2)); + int ret = memcmp(str1.constData(), str2.constData(), qMin(l1, l2)); if (ret != 0) return ret; @@ -995,6 +995,8 @@ QByteArray &QByteArray::operator=(const char *str) /*! \fn QByteArray::operator const char *() const \fn QByteArray::operator const void *() const + \obsolete Use constData() instead. + Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. The data is '\\0'-terminated. The pointer remains valid as long @@ -2751,7 +2753,7 @@ QDataStream &operator<<(QDataStream &out, const QByteArray &ba) out << (quint32)0xffffffff; return out; } - return out.writeBytes(ba, ba.size()); + return out.writeBytes(ba.constData(), ba.size()); } /*! \relates QByteArray diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 8202097da5..09c43988fd 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -207,8 +207,10 @@ public: void squeeze(); #ifndef QT_NO_CAST_FROM_BYTEARRAY - operator const char *() const; - operator const void *() const; +#if QT_DEPRECATED_SINCE(5, 0) + QT_DEPRECATED operator const char *() const { return constData(); } + QT_DEPRECATED operator const void *() const { return constData(); } +#endif #endif char *data(); const char *data() const; @@ -415,12 +417,6 @@ inline char QByteArray::operator[](uint i) const inline bool QByteArray::isEmpty() const { return d->size == 0; } -#ifndef QT_NO_CAST_FROM_BYTEARRAY -inline QByteArray::operator const char *() const -{ return d->data(); } -inline QByteArray::operator const void *() const -{ return d->data(); } -#endif inline char *QByteArray::data() { detach(); return d->data(); } inline const char *QByteArray::data() const diff --git a/src/corelib/tools/qlocale_unix.cpp b/src/corelib/tools/qlocale_unix.cpp index 6ace96f771..f2876912b4 100644 --- a/src/corelib/tools/qlocale_unix.cpp +++ b/src/corelib/tools/qlocale_unix.cpp @@ -98,7 +98,7 @@ QLocale QSystemLocale::fallbackLocale() const lang = qgetenv("LC_NUMERIC"); if (lang.isEmpty()) lang = qgetenv("LANG"); - return QLocale(QLatin1String(lang)); + return QLocale(QString::fromLatin1(lang)); } QVariant QSystemLocale::query(QueryType type, QVariant in) const diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 704545667c..14b8782dae 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -4761,7 +4761,7 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1, } // else fall through # endif // declared in - int delta = strcoll(toLocal8Bit_helper(data1, length1), toLocal8Bit_helper(data2, length2)); + int delta = strcoll(toLocal8Bit_helper(data1, length1).constData(), toLocal8Bit_helper(data2, length2).constData()); if (delta == 0) delta = ucstrcmp(data1, length1, data2, length2); return delta; diff --git a/src/tools/rcc/rcc.cpp b/src/tools/rcc/rcc.cpp index 5fbda35fc6..bfb3206778 100644 --- a/src/tools/rcc/rcc.cpp +++ b/src/tools/rcc/rcc.cpp @@ -681,7 +681,7 @@ bool RCCResourceLibrary::output(QIODevice &outDevice, QIODevice &errorDevice) m_errorDevice->write("Could not write footer\n"); return false; } - outDevice.write(m_out, m_out.size()); + outDevice.write(m_out.constData(), m_out.size()); return true; } diff --git a/src/tools/uic/cpp/cppwriteicondata.cpp b/src/tools/uic/cpp/cppwriteicondata.cpp index 39ce52f4b3..082961cfe6 100644 --- a/src/tools/uic/cpp/cppwriteicondata.cpp +++ b/src/tools/uic/cpp/cppwriteicondata.cpp @@ -176,8 +176,8 @@ void WriteIconData::writeImage(QTextStream &output, const QString &indent, void WriteIconData::writeImage(QIODevice &output, DomImage *image) { - QByteArray array = transformImageData(image->elementData()->text()); - output.write(array, array.size()); + const QByteArray array = transformImageData(image->elementData()->text()); + output.write(array.constData(), array.size()); } } // namespace CPP diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index c16fd03613..38a44024aa 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -5682,24 +5682,24 @@ static QByteArray encodeEntity(const QByteArray& str) QByteArray tmp(str); int len = tmp.size(); int i = 0; - const char* d = tmp.data(); + const char* d = tmp.constData(); while (i < len) { if (d[i] == '%'){ tmp.replace(i, 1, "<"); - d = tmp; + d = tmp.constData(); len += 4; i += 5; } else if (d[i] == '"') { tmp.replace(i, 1, """); - d = tmp; + d = tmp.constData(); len += 4; i += 5; } else if (d[i] == '&' && i + 1 < len && d[i+1] == '#') { // Dont encode < or " or &custom;. // Only encode character references tmp.replace(i, 1, "&"); - d = tmp; + d = tmp.constData(); len += 4; i += 5; } else { diff --git a/src/xml/sax/qxml.cpp b/src/xml/sax/qxml.cpp index 2a41ce3426..75e2fdb2e7 100644 --- a/src/xml/sax/qxml.cpp +++ b/src/xml/sax/qxml.cpp @@ -1612,7 +1612,7 @@ QString QXmlInputSource::fromRawData(const QByteArray &data, bool beginning) d->encMapper = codec->makeDecoder(); } - QString input = d->encMapper->toUnicode(data, data.size()); + QString input = d->encMapper->toUnicode(data.constData(), data.size()); if (d->lookingForEncodingDecl) { d->encodingDeclChars += input; @@ -1633,9 +1633,9 @@ QString QXmlInputSource::fromRawData(const QByteArray &data, bool beginning) input.clear(); // prime the decoder with the data so far - d->encMapper->toUnicode(d->encodingDeclBytes, d->encodingDeclBytes.size()); + d->encMapper->toUnicode(d->encodingDeclBytes.constData(), d->encodingDeclBytes.size()); // now feed it the new data - input = d->encMapper->toUnicode(data, data.size()); + input = d->encMapper->toUnicode(data.constData(), data.size()); } } } -- cgit v1.2.3