QByteArray: deprecate QT_NO_CAST_FROM_BYTEARRAY-protected operators
authorMarc Mutz <marc.mutz@kdab.com>
Wed, 22 Feb 2012 15:17:30 +0000 (16:17 +0100)
committerQt by Nokia <qt-info@nokia.com>
Sun, 26 Feb 2012 08:14:11 +0000 (09:14 +0100)
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 <faure@kde.org>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
18 files changed:
qmake/generators/integrity/gbuild.cpp
qmake/generators/mac/pbuilder_pbx.cpp
qmake/generators/makefiledeps.cpp
qmake/generators/win32/msvc_objectmodel.cpp
qmake/generators/win32/msvc_objectmodel.h
qmake/project.cpp
src/corelib/codecs/qtextcodec.cpp
src/corelib/io/qfilesystemengine_unix.cpp
src/corelib/io/qsettings.cpp
src/corelib/kernel/qvariant.cpp
src/corelib/tools/qbytearray.cpp
src/corelib/tools/qbytearray.h
src/corelib/tools/qlocale_unix.cpp
src/corelib/tools/qstring.cpp
src/tools/rcc/rcc.cpp
src/tools/uic/cpp/cppwriteicondata.cpp
src/xml/dom/qdom.cpp
src/xml/sax/qxml.cpp

index c72c120..895df24 100644 (file)
@@ -213,7 +213,7 @@ GBuildMakefileGenerator::write()
         dllbase += DLLOFFSET;
     }
 
-    warn_msg(WarnParser, Option::output.fileName().toAscii());
+    warn_msg(WarnParser, Option::output.fileName().toAscii().constData());
     QTextStream t(&Option::output);
     QString primaryTarget(project->values("QMAKE_CXX").at(0));
 
@@ -425,7 +425,7 @@ GBuildMakefileGenerator::openOutput(QFile &file, const QString &build) const
         outputName += QDir::separator();
         outputName += fileInfo(project->projectFile()).baseName();
         outputName += projectSuffix();
-        warn_msg(WarnParser, outputName.toAscii());
+        warn_msg(WarnParser, outputName.toAscii().constData());
         file.setFileName(outputName);
     }
     debug_msg(1, "file is %s", file.fileName().toLatin1().constData());
index b78ebd1..2f6b30f 100644 (file)
@@ -1374,7 +1374,7 @@ ProjectBuilderMakefileGenerator::writeMakeParts(QTextStream &t)
     }
     tmp = project->values("QMAKE_PBX_VARS");
     for(int i = 0; i < tmp.count(); i++) {
-        QString var = tmp[i], val = qgetenv(var.toLatin1());
+        QString var = tmp[i], val = QString::fromAscii(qgetenv(var.toLatin1().constData()));
         if(val.isEmpty() && var == "TB")
             val = "/usr/bin/";
         t << "\t\t\t\t" << writeSettings(var, escapeFilePath(val)) << ";" << "\n";
index d28d48b..642dd97 100644 (file)
@@ -185,9 +185,9 @@ SourceFile *SourceFiles::lookupFile(const char *file)
 
 void SourceFiles::addFile(SourceFile *p, const char *k, bool own_file)
 {
-    QByteArray ba = p->file.local().toLatin1();
+    const QByteArray ba = p->file.local().toLatin1();
     if(!k)
-        k = ba;
+        k = ba.constData();
     int h = hash(k) % num_nodes;
     SourceFileNode *pn = new SourceFileNode;
     pn->own_file = own_file;
index 81f541f..e0f55a6 100644 (file)
@@ -1615,7 +1615,7 @@ bool VCLinkerTool::parseOption(const char* option)
         {
             // Split up in subsystem, and version number
             QStringList both = QString(option+11).split(",");
-            switch (elfHash(both[0].toLatin1())) {
+            switch (elfHash(both[0].toLatin1().constData())) {
             case 0x8438445: // CONSOLE
                 SubSystem = subSystemConsole;
                 break;
index 8b1cf51..37d923f 100644 (file)
@@ -477,7 +477,7 @@ protected:
 public:
     void parseOptions(QStringList& options) {
         for (QStringList::ConstIterator it=options.begin(); (it!=options.end()); it++)
-            parseOption((*it).toLatin1());
+            parseOption((*it).toLatin1().constData());
     }
     static QStringList fixCommandLine(const QString &input);
 };
index 2029e06..379854e 100644 (file)
@@ -1520,7 +1520,7 @@ QMakeProject::resolveSpec(QString *spec, const QString &qmakespec)
         if (*spec == "default") {
 #ifdef Q_OS_UNIX
             char buffer[1024];
-            int l = readlink(qmakespec.toLatin1(), buffer, 1023);
+            int l = readlink(qmakespec.toLatin1().constData(), buffer, 1023);
             if (l != -1) {
                 buffer[l] = '\0';
                 *spec = QString::fromLatin1(buffer);
@@ -2070,7 +2070,7 @@ QMakeProject::doProjectExpand(QString func, QList<QStringList> args_list,
             if(args.count() > 1)
                 singleLine = (args[1].toLower() == "true");
             QString output;
-            FILE *proc = QT_POPEN(args[0].toLatin1(), "r");
+            FILE *proc = QT_POPEN(args[0].toLatin1().constData(), "r");
             while(proc && !feof(proc)) {
                 int read_in = int(fread(buff, 1, 255, proc));
                 if(!read_in)
@@ -3150,12 +3150,12 @@ QStringList &QMakeProject::values(const QString &_var, QHash<QString, QStringLis
       else if(var.startsWith(QLatin1String("QMAKE_TARGET."))) {
             QString ret, type = var.mid(13);
             if(type == "arch") {
-                QString paths = qgetenv("PATH");
-                QString vcBin64 = qgetenv("VCINSTALLDIR");
+                QString paths = QString::fromLocal8Bit(qgetenv("PATH"));
+                QString vcBin64 = QString::fromLocal8Bit(qgetenv("VCINSTALLDIR"));
                 if (!vcBin64.endsWith('\\'))
                     vcBin64.append('\\');
                 vcBin64.append("bin\\amd64");
-                QString vcBinX86_64 = qgetenv("VCINSTALLDIR");
+                QString vcBinX86_64 = QString::fromLocal8Bit(qgetenv("VCINSTALLDIR"));
                 if (!vcBinX86_64.endsWith('\\'))
                     vcBinX86_64.append('\\');
                 vcBinX86_64.append("bin\\x86_amd64");
index 82e5c9a..e49ebfb 100644 (file)
@@ -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());
         }
 
     }
index 5e466e4..e8ff610 100644 (file)
@@ -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;
 }
 
index 2021c42..f743c59 100644 (file)
@@ -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('[');
index 4e7fd94..2f67ae9 100644 (file)
@@ -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;
index c74c619..1d37f57 100644 (file)
@@ -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
index 8202097..09c4398 100644 (file)
@@ -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
index 6ace96f..f287691 100644 (file)
@@ -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
index 7045456..14b8782 100644 (file)
@@ -4761,7 +4761,7 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1,
     } // else fall through
 #  endif
     // declared in <string.h>
-    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;
index 5fbda35..bfb3206 100644 (file)
@@ -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;
 }
 
index 39ce52f..082961c 100644 (file)
@@ -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
index c16fd03..38a4402 100644 (file)
@@ -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, "&#60;");
-            d = tmp;
+            d = tmp.constData();
             len += 4;
             i += 5;
         }
         else if (d[i] == '"') {
             tmp.replace(i, 1, "&#34;");
-            d = tmp;
+            d = tmp.constData();
             len += 4;
             i += 5;
         } else if (d[i] == '&' && i + 1 < len && d[i+1] == '#') {
             // Dont encode &lt; or &quot; or &custom;.
             // Only encode character references
             tmp.replace(i, 1, "&#38;");
-            d = tmp;
+            d = tmp.constData();
             len += 4;
             i += 5;
         } else {
index 2a41ce3..75e2fdb 100644 (file)
@@ -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());
                 }
             }
         }