Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 41.68 KB | None | 0 0
  1. From 89ca2e569932992934ff6d296993ab9a341744c1 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  3. Date: Fri, 5 Aug 2011 10:30:32 +0200
  4. Subject: [PATCH 01/14] Modulus of negative dividends is undefined or negative
  5.  
  6. ... depending on who you ask. Since it is possible for applicationPid to
  7. return negative values this means we would introduce garbage ['()*+,-./]
  8. in the generated filenames.
  9.  
  10. Reviewed-by: Denis Dzyubenko
  11. ---
  12. src/corelib/io/qtemporaryfile.cpp |    2 +-
  13.  1 files changed, 1 insertions(+), 1 deletions(-)
  14.  
  15. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  16. index 0855d93..5bc07cb 100644
  17. --- a/src/corelib/io/qtemporaryfile.cpp
  18. +++ b/src/corelib/io/qtemporaryfile.cpp
  19. @@ -118,7 +118,7 @@ static int createFileFromTemplate(char *const path,
  20.          char *rIter = placeholderEnd;
  21.  
  22.  #if defined(QT_BUILD_CORE_LIB)
  23. -        qint64 pid = QCoreApplication::applicationPid();
  24. +        quint64 pid = quint64(QCoreApplication::applicationPid());
  25.          do {
  26.              *--rIter = (pid % 10) + '0';
  27.              pid /= 10;
  28. --
  29. 1.7.5.2
  30.  
  31.  
  32. From e96177cf94dad186a0fdc683338f10048617a9ee Mon Sep 17 00:00:00 2001
  33. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  34. Date: Fri, 5 Aug 2011 10:32:08 +0200
  35. Subject: [PATCH 02/14] Changed if/if/else/if/eleven chain to switch
  36.  
  37. Inlined isdigit in switch statement. Removed unused #includes.
  38. Documented unreachable segment with code (Q_ASSERT).
  39.  
  40. Reviewed-by: Denis Dzyubenko
  41. ---
  42. src/corelib/io/qtemporaryfile.cpp |   37 ++++++++++++++++++++++---------------
  43.  1 files changed, 22 insertions(+), 15 deletions(-)
  44.  
  45. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  46. index 5bc07cb..9753332 100644
  47. --- a/src/corelib/io/qtemporaryfile.cpp
  48. +++ b/src/corelib/io/qtemporaryfile.cpp
  49. @@ -53,10 +53,6 @@
  50.  #  include <errno.h>
  51.  #endif
  52.  
  53. -#include <stdlib.h>
  54. -#include <time.h>
  55. -#include <ctype.h>
  56. -
  57.  #if defined(Q_OS_UNIX)
  58.  # include "private/qcore_unix_p.h"      // overrides QT_OPEN
  59.  #endif
  60. @@ -153,23 +149,34 @@ static int createFileFromTemplate(char *const path,
  61.          for (char *iter = placeholderStart;;) {
  62.              // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
  63.              // String progression: "ZZaiC" => "aabiC"
  64. -            if (*iter == 'Z') {
  65. -                *iter++ = 'a';
  66. -                if (iter == placeholderEnd)
  67. -                    return -1;
  68. -            } else {
  69. -                if (isdigit(*iter))
  70. +            switch (*iter) {
  71. +                case 'Z':
  72. +                    // Rollover, advance next character
  73.                      *iter = 'a';
  74. -                else if (*iter == 'z') /* inc from z to A */
  75. +                    if (++iter == placeholderEnd)
  76. +                        return -1;
  77. +
  78. +                    continue;
  79. +
  80. +                case '0': case '1': case '2': case '3': case '4':
  81. +                case '5': case '6': case '7': case '8': case '9':
  82. +                    *iter = 'a';
  83. +                    break;
  84. +
  85. +                case 'z':
  86. +                    // increment 'z' to 'A'
  87.                      *iter = 'A';
  88. -                else {
  89. +                    break;
  90. +
  91. +                default:
  92.                      ++*iter;
  93. -                }
  94. -                break;
  95. +                    break;
  96.              }
  97. +            break;
  98.          }
  99.      }
  100. -    /*NOTREACHED*/
  101. +
  102. +    Q_ASSERT(false);
  103.  }
  104.  
  105.  //************* QTemporaryFileEngine
  106. --
  107. 1.7.5.2
  108.  
  109.  
  110. From 5f87b94a0d58eba01401d17c97c38b13a739a2b2 Mon Sep 17 00:00:00 2001
  111. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  112. Date: Fri, 5 Aug 2011 10:32:25 +0200
  113. Subject: [PATCH 03/14] Don't convert template's path separators again
  114.  
  115. ---
  116. src/corelib/io/qtemporaryfile.cpp |    2 +-
  117.  1 files changed, 1 insertions(+), 1 deletions(-)
  118.  
  119. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  120. index 9753332..aae3eb6 100644
  121. --- a/src/corelib/io/qtemporaryfile.cpp
  122. +++ b/src/corelib/io/qtemporaryfile.cpp
  123. @@ -330,7 +330,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  124.          return true;
  125.      }
  126.  
  127. -    d->fileEntry = QFileSystemEntry(template_);
  128. +    d->fileEntry = QFileSystemEntry(template_, QFileSystemEntry::FromInternalPath());
  129.      return false;
  130.  #endif
  131.  }
  132. --
  133. 1.7.5.2
  134.  
  135.  
  136. From f11956ba7806e648b461ce74ac2e332300ef1338 Mon Sep 17 00:00:00 2001
  137. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  138. Date: Fri, 5 Aug 2011 10:32:51 +0200
  139. Subject: [PATCH 04/14] Use fromLocal8Bit for reversing toLocal8Bit
  140.  
  141. path is converted to 8-bit encoding using toLocal8Bit in
  142. QTemporaryFileEngine::open. The reverse operation should be used here.
  143. ---
  144. src/corelib/io/qtemporaryfile.cpp |    2 +-
  145.  1 files changed, 1 insertions(+), 1 deletions(-)
  146.  
  147. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  148. index aae3eb6..d457601 100644
  149. --- a/src/corelib/io/qtemporaryfile.cpp
  150. +++ b/src/corelib/io/qtemporaryfile.cpp
  151. @@ -141,7 +141,7 @@ static int createFileFromTemplate(char *const path,
  152.                  return -1;
  153.          }
  154.  #else
  155. -        if (!QFileInfo(QLatin1String(path)).exists())
  156. +        if (!QFileInfo(QString::fromLocal8Bit(path)).exists())
  157.              return 1;
  158.  #endif
  159.  
  160. --
  161. 1.7.5.2
  162.  
  163.  
  164. From 4403413edc7dcd2cb7c238efb3ea7d7797cc386f Mon Sep 17 00:00:00 2001
  165. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  166. Date: Fri, 5 Aug 2011 10:35:55 +0200
  167. Subject: [PATCH 05/14] Encapsulate pointer manipulations to
  168.  createFileTemplate function
  169.  
  170. , where we actually control how we use the pointers. Reduce some code
  171. duplication in #ifdefs.
  172.  
  173. Reviewed-by: ?
  174. ---
  175. src/corelib/io/qtemporaryfile.cpp |   33 ++++++++++++++++++---------------
  176.  1 files changed, 18 insertions(+), 15 deletions(-)
  177.  
  178. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  179. index d457601..9228f94 100644
  180. --- a/src/corelib/io/qtemporaryfile.cpp
  181. +++ b/src/corelib/io/qtemporaryfile.cpp
  182. @@ -96,18 +96,22 @@ QT_BEGIN_NAMESPACE
  183.      \internal
  184.  
  185.      Generates a unique file path and returns a native handle to the open file.
  186. -    \a path is used as a template when generating unique paths,
  187. -    \a placeholderStart and \a placeholderEnd delimit the sub-string that will
  188. -    be randomized.
  189. +    \a path is used as a template when generating unique paths, \a pos
  190. +    identifies the position of the first character that will be replaced in the
  191. +    template and \a length the number of characters that may be substituted.
  192.  
  193.      Returns an open handle to the newly created file if successful, an invalid
  194.      handle otherwise. In both cases, the string in \a path will be changed and
  195.      contain the generated path name.
  196.  */
  197. -static int createFileFromTemplate(char *const path,
  198. -        char *const placeholderStart, char *const placeholderEnd)
  199. +static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  200.  {
  201. -    Q_ASSERT(placeholderEnd > placeholderStart);
  202. +    Q_ASSERT(length != 0);
  203. +    Q_ASSERT(pos < size_t(path.length()));
  204. +    Q_ASSERT(length < size_t(path.length()) - pos);
  205. +
  206. +    char *const placeholderStart = path.data() + pos;
  207. +    char *const placeholderEnd = placeholderStart + length;
  208.  
  209.      // Initialize placeholder with random chars + PID.
  210.      {
  211. @@ -134,14 +138,16 @@ static int createFileFromTemplate(char *const path,
  212.          // Atomically create file and obtain handle
  213.  #ifndef Q_OS_WIN
  214.          {
  215. -            int fd = QT_OPEN(path, QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE, 0600);
  216. +            int fd = QT_OPEN(path.constData(),
  217. +                    QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
  218. +                    0600);
  219.              if (fd != -1)
  220.                  return fd;
  221.              if (errno != EEXIST)
  222.                  return -1;
  223.          }
  224.  #else
  225. -        if (!QFileInfo(QString::fromLocal8Bit(path)).exists())
  226. +        if (!QFileInfo(QString::fromLocal8Bit(path.constData(), path.length())).exists())
  227.              return 1;
  228.  #endif
  229.  
  230. @@ -295,10 +301,9 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  231.      else
  232.          filename.insert(phPos + phLength, suffix.toLocal8Bit());
  233.  
  234. -    char *path = filename.data();
  235. +    int fd = createFileFromTemplate(filename, phPos, phLength);
  236.  
  237.  #ifndef Q_OS_WIN
  238. -    int fd = createFileFromTemplate(path, path + phPos, path + phPos + phLength);
  239.      if (fd != -1) {
  240.          // First open the fd as an external file descriptor to
  241.          // initialize the engine properly.
  242. @@ -308,7 +313,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  243.              d->closeFileHandle = true;
  244.  
  245.              // Restore the file names (open() resets them).
  246. -            d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(path, filename.length())); //note that filename is NOT a native path
  247. +            d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename.constData(), filename.length())); //note that filename is NOT a native path
  248.              filePathIsTemplate = false;
  249.              return true;
  250.          }
  251. @@ -318,13 +323,11 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  252.      setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  253.      return false;
  254.  #else
  255. -    if (createFileFromTemplate(path, path + phPos, path + phPos + phLength) == -1) {
  256. +    if (fd == -1)
  257.          return false;
  258. -    }
  259.  
  260.      QString template_ = d->fileEntry.filePath();
  261. -    d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(path, filename.length()));
  262. -
  263. +    d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename.constData(), filename.length()));
  264.      if (QFSFileEngine::open(openMode)) {
  265.          filePathIsTemplate = false;
  266.          return true;
  267. --
  268. 1.7.5.2
  269.  
  270.  
  271. From 8fc58dbb80f7d9d1a3782c90a2cda4efa5787ff0 Mon Sep 17 00:00:00 2001
  272. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  273. Date: Fri, 5 Aug 2011 10:57:19 +0200
  274. Subject: [PATCH 06/14] Use QStringBuilder when copying template for
  275.  modification
  276.  
  277. This avoids modifying the original string in the case where a
  278. placeholder marker is not found. By marking the variable const we
  279. further avoid checks on the reference count and detaches, also allowing
  280. us to safely reuse it later in the function.
  281.  
  282. The new approach also fixes an issue where suffix wasn't empty, but the
  283. toLocal8Bit conversion would be. This resulted in a buffer overflow
  284. inside createFileFromTemplate.
  285.  
  286. Reviewed-by: ?
  287. ---
  288. src/corelib/io/qtemporaryfile.cpp |   62 ++++++++++++++++++++++++++++---------
  289.  1 files changed, 47 insertions(+), 15 deletions(-)
  290.  
  291. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  292. index 9228f94..1d3f7ca 100644
  293. --- a/src/corelib/io/qtemporaryfile.cpp
  294. +++ b/src/corelib/io/qtemporaryfile.cpp
  295. @@ -45,6 +45,7 @@
  296.  
  297.  #include "qplatformdefs.h"
  298.  #include "qabstractfileengine.h"
  299. +#include "qstringbuilder.h"
  300.  #include "private/qfile_p.h"
  301.  #include "private/qabstractfileengine_p.h"
  302.  #include "private/qfsfileengine_p.h"
  303. @@ -63,6 +64,38 @@
  304.  
  305.  QT_BEGIN_NAMESPACE
  306.  
  307. +struct Placeholder
  308. +{
  309. +    Placeholder(int size)
  310. +        : size_(size)
  311. +    {
  312. +    }
  313. +
  314. +    int size() const
  315. +    {
  316. +        return size_;
  317. +    }
  318. +
  319. +private:
  320. +    int size_;
  321. +};
  322. +
  323. +template <>
  324. +struct QConcatenable<Placeholder>
  325. +{
  326. +    typedef Placeholder type;
  327. +    typedef QByteArray ConvertTo;
  328. +    enum { ExactSize = true };
  329. +    static int size(const Placeholder &p) { return p.size(); }
  330. +
  331. +    template <class CharT>
  332. +    static inline void appendTo(const Placeholder &p, CharT *&out)
  333. +    {
  334. +        // Uninitialized
  335. +        out += p.size();
  336. +    }
  337. +};
  338. +
  339.  /*
  340.   * Copyright (c) 1987, 1993
  341.   * The Regents of the University of California.  All rights reserved.
  342. @@ -261,7 +294,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  343.      if (!filePathIsTemplate)
  344.          return QFSFileEngine::open(openMode);
  345.  
  346. -    QString qfilename = d->fileEntry.filePath();
  347. +    const QString qfilename = d->fileEntry.filePath();
  348.  
  349.      // Find placeholder string.
  350.      uint phPos = qfilename.length();
  351. @@ -284,22 +317,22 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  352.          phLength = 0;
  353.      }
  354.  
  355. -    QStringRef prefix, suffix;
  356. +    QByteArray filename;
  357. +
  358.      if (phLength < 6) {
  359. -        qfilename += QLatin1Char('.');
  360. -        prefix = QStringRef(&qfilename);
  361. +        filename = qfilename.toLocal8Bit();
  362. +
  363. +        phPos = filename.length() + 1; // Account for added dot in prefix
  364.          phLength = 6;
  365. +        filename = filename % '.' % Placeholder(phLength);
  366.      } else {
  367. -        prefix = qfilename.leftRef(phPos);
  368. -        suffix = qfilename.midRef(phPos + phLength);
  369. -    }
  370. +        QByteArray prefix, suffix;
  371. +        prefix = qfilename.leftRef(phPos).toLocal8Bit();
  372. +        suffix = qfilename.midRef(phPos + phLength).toLocal8Bit();
  373.  
  374. -    QByteArray filename = prefix.toLocal8Bit();
  375. -    phPos = filename.length();
  376. -    if (suffix.isEmpty())
  377. -        filename.resize(phPos + phLength);
  378. -    else
  379. -        filename.insert(phPos + phLength, suffix.toLocal8Bit());
  380. +        phPos = prefix.length();
  381. +        filename = prefix % Placeholder(phLength) % suffix;
  382. +    }
  383.  
  384.      int fd = createFileFromTemplate(filename, phPos, phLength);
  385.  
  386. @@ -326,14 +359,13 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  387.      if (fd == -1)
  388.          return false;
  389.  
  390. -    QString template_ = d->fileEntry.filePath();
  391.      d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename.constData(), filename.length()));
  392.      if (QFSFileEngine::open(openMode)) {
  393.          filePathIsTemplate = false;
  394.          return true;
  395.      }
  396.  
  397. -    d->fileEntry = QFileSystemEntry(template_, QFileSystemEntry::FromInternalPath());
  398. +    d->fileEntry = QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath());
  399.      return false;
  400.  #endif
  401.  }
  402. --
  403. 1.7.5.2
  404.  
  405.  
  406. From 7cabe00f8b189dc592d6df0975e6952012f29674 Mon Sep 17 00:00:00 2001
  407. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  408. Date: Fri, 5 Aug 2011 10:39:53 +0200
  409. Subject: [PATCH 07/14] Make Symbian follow Windows code in temporary path
  410.  generation
  411.  
  412. On the one hand, we stop using OpenC here. On the other, we no longer
  413. use an atomic create and obtain file handle API -- just as we don't on
  414. Windows yet.
  415.  
  416. This is a stepping stone to removing back and forth conversions of path
  417. names when generating unique names and also towards the use of native
  418. APIs for creating and obtaining a file handle atomically.
  419.  
  420. Reviewed-by: ?
  421. ---
  422. src/corelib/io/qtemporaryfile.cpp |   13 +++++--------
  423.  1 files changed, 5 insertions(+), 8 deletions(-)
  424.  
  425. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  426. index 1d3f7ca..b079d3e 100644
  427. --- a/src/corelib/io/qtemporaryfile.cpp
  428. +++ b/src/corelib/io/qtemporaryfile.cpp
  429. @@ -50,12 +50,9 @@
  430.  #include "private/qabstractfileengine_p.h"
  431.  #include "private/qfsfileengine_p.h"
  432.  
  433. -#if !defined(Q_OS_WINCE)
  434. -#  include <errno.h>
  435. -#endif
  436. -
  437. -#if defined(Q_OS_UNIX)
  438. -# include "private/qcore_unix_p.h"      // overrides QT_OPEN
  439. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  440. +#include "private/qcore_unix_p.h"       // overrides QT_OPEN
  441. +#include <errno.h>
  442.  #endif
  443.  
  444.  #if defined(QT_BUILD_CORE_LIB)
  445. @@ -169,7 +166,7 @@ static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  446.  
  447.      for (;;) {
  448.          // Atomically create file and obtain handle
  449. -#ifndef Q_OS_WIN
  450. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  451.          {
  452.              int fd = QT_OPEN(path.constData(),
  453.                      QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
  454. @@ -336,7 +333,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  455.  
  456.      int fd = createFileFromTemplate(filename, phPos, phLength);
  457.  
  458. -#ifndef Q_OS_WIN
  459. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  460.      if (fd != -1) {
  461.          // First open the fd as an external file descriptor to
  462.          // initialize the engine properly.
  463. --
  464. 1.7.5.2
  465.  
  466.  
  467. From 07183c34f31fea7f4d5cd12abee863239443eb0c Mon Sep 17 00:00:00 2001
  468. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  469. Date: Fri, 5 Aug 2011 10:40:46 +0200
  470. Subject: [PATCH 08/14] Avoid spurious detaching in
  471.  QDir::to/fromNativeSeparators
  472.  
  473. The new code avoids non-const detaching operations until needed and uses
  474. a pointer into the "raw" QChar data from then on, thus skipping unneeded
  475. checks on the reference count for further detaching.
  476.  
  477. These functions are used all the time by the file system classes so this
  478. small optimization won't hurt. In particular, it will help users who
  479. already use '/' when passing paths into Qt.
  480.  
  481. Reviewed-by: Peter Hartmann
  482. ---
  483. src/corelib/io/qdir.cpp |   38 ++++++++++++++++++++++++++++----------
  484.  1 files changed, 28 insertions(+), 10 deletions(-)
  485.  
  486. diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp
  487. index b31cf69..f9196e0 100644
  488. --- a/src/corelib/io/qdir.cpp
  489. +++ b/src/corelib/io/qdir.cpp
  490. @@ -802,14 +802,23 @@ QString QDir::convertSeparators(const QString &pathName)
  491.  */
  492.  QString QDir::toNativeSeparators(const QString &pathName)
  493.  {
  494. -    QString n(pathName);
  495.  #if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
  496. -    for (int i = 0; i < (int)n.length(); ++i) {
  497. -        if (n[i] == QLatin1Char('/'))
  498. -            n[i] = QLatin1Char('\\');
  499. +    int i = pathName.indexOf(QLatin1Char('/'));
  500. +    if (i != -1) {
  501. +        QString n(pathName);
  502. +
  503. +        QChar * const data = n.data();
  504. +        data[i++] = QLatin1Char('\\');
  505. +
  506. +        for (; i < n.length(); ++i) {
  507. +            if (data[i] == QLatin1Char('/'))
  508. +                data[i] = QLatin1Char('\\');
  509. +        }
  510. +
  511. +        return n;
  512.      }
  513.  #endif
  514. -    return n;
  515. +    return pathName;
  516.  }
  517.  
  518.  /*!
  519. @@ -826,14 +835,23 @@ QString QDir::toNativeSeparators(const QString &pathName)
  520.  */
  521.  QString QDir::fromNativeSeparators(const QString &pathName)
  522.  {
  523. -    QString n(pathName);
  524.  #if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) || defined(Q_OS_SYMBIAN)
  525. -    for (int i = 0; i < (int)n.length(); ++i) {
  526. -        if (n[i] == QLatin1Char('\\'))
  527. -            n[i] = QLatin1Char('/');
  528. +    int i = pathName.indexOf(QLatin1Char('\\'));
  529. +    if (i != -1) {
  530. +        QString n(pathName);
  531. +
  532. +        QChar * const data = n.data();
  533. +        data[i++] = QLatin1Char('/');
  534. +
  535. +        for (; i < n.length(); ++i) {
  536. +            if (data[i] == QLatin1Char('\\'))
  537. +                data[i] = QLatin1Char('/');
  538. +        }
  539. +
  540. +        return n;
  541.      }
  542.  #endif
  543. -    return n;
  544. +    return pathName;
  545.  }
  546.  
  547.  /*!
  548. --
  549. 1.7.5.2
  550.  
  551.  
  552. From a3e2fdb4382cf460c6fed2caca06a0a99b9d7f1b Mon Sep 17 00:00:00 2001
  553. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  554. Date: Fri, 5 Aug 2011 10:58:08 +0200
  555. Subject: [PATCH 09/14] Minimize encoding conversions when generating unique
  556.  file name
  557.  
  558. With minor adjustments, createFileFromTemplate is made to work directly
  559. on (UTF-16) QString data, which is already in the native encoding for
  560. Windows and Symbian. This is possible because the function only fills
  561. out the placeholder sub-string, without touching adjacent characters.
  562.  
  563. This eliminates unnecessary conversions on those platforms.
  564.  
  565. Reviewed-by: ?
  566. ---
  567. src/corelib/io/qtemporaryfile.cpp |   69 +++++++++++++++++++++++++++++--------
  568.  1 files changed, 54 insertions(+), 15 deletions(-)
  569.  
  570. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  571. index b079d3e..e4c53aa 100644
  572. --- a/src/corelib/io/qtemporaryfile.cpp
  573. +++ b/src/corelib/io/qtemporaryfile.cpp
  574. @@ -61,6 +61,33 @@
  575.  
  576.  QT_BEGIN_NAMESPACE
  577.  
  578. +#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
  579. +typedef ushort Char;
  580. +
  581. +static inline Char Latin1Char(char ch)
  582. +{
  583. +    return ushort(uchar(ch));
  584. +}
  585. +
  586. +template <>
  587. +struct QConcatenable<Char>
  588. +{
  589. +    typedef Char type;
  590. +    typedef QString ConvertTo;
  591. +    enum { ExactSize = true };
  592. +    static int size(const Char &) { return 1; }
  593. +
  594. +    static inline void appendTo(const Char &u16, QChar *&out)
  595. +    {
  596. +        *out++ = QChar(u16);
  597. +    }
  598. +};
  599. +
  600. +#else // POSIX
  601. +typedef char Char;
  602. +typedef char Latin1Char;
  603. +#endif
  604. +
  605.  struct Placeholder
  606.  {
  607.      Placeholder(int size)
  608. @@ -134,23 +161,24 @@ struct QConcatenable<Placeholder>
  609.      handle otherwise. In both cases, the string in \a path will be changed and
  610.      contain the generated path name.
  611.  */
  612. -static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  613. +static int createFileFromTemplate(QFileSystemEntry::NativePath &path,
  614. +        size_t pos, size_t length)
  615.  {
  616.      Q_ASSERT(length != 0);
  617.      Q_ASSERT(pos < size_t(path.length()));
  618.      Q_ASSERT(length < size_t(path.length()) - pos);
  619.  
  620. -    char *const placeholderStart = path.data() + pos;
  621. -    char *const placeholderEnd = placeholderStart + length;
  622. +    Char *const placeholderStart = (Char *)path.data() + pos;
  623. +    Char *const placeholderEnd = placeholderStart + length;
  624.  
  625.      // Initialize placeholder with random chars + PID.
  626.      {
  627. -        char *rIter = placeholderEnd;
  628. +        Char *rIter = placeholderEnd;
  629.  
  630.  #if defined(QT_BUILD_CORE_LIB)
  631.          quint64 pid = quint64(QCoreApplication::applicationPid());
  632.          do {
  633. -            *--rIter = (pid % 10) + '0';
  634. +            *--rIter = Latin1Char((pid % 10) + '0');
  635.              pid /= 10;
  636.          } while (rIter != placeholderStart && pid != 0);
  637.  #endif
  638. @@ -158,9 +186,9 @@ static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  639.          while (rIter != placeholderStart) {
  640.              char ch = char((qrand() & 0xffff) % (26 + 26));
  641.              if (ch < 26)
  642. -                *--rIter = ch + 'A';
  643. +                *--rIter = Latin1Char(ch + 'A');
  644.              else
  645. -                *--rIter = ch - 26 + 'a';
  646. +                *--rIter = Latin1Char(ch - 26 + 'a');
  647.          }
  648.      }
  649.  
  650. @@ -177,18 +205,18 @@ static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  651.                  return -1;
  652.          }
  653.  #else
  654. -        if (!QFileInfo(QString::fromLocal8Bit(path.constData(), path.length())).exists())
  655. +        if (!QFileInfo(path).exists())
  656.              return 1;
  657.  #endif
  658.  
  659.          /* tricky little algorwwithm for backward compatibility */
  660. -        for (char *iter = placeholderStart;;) {
  661. +        for (Char *iter = placeholderStart;;) {
  662.              // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
  663.              // String progression: "ZZaiC" => "aabiC"
  664. -            switch (*iter) {
  665. +            switch (char(*iter)) {
  666.                  case 'Z':
  667.                      // Rollover, advance next character
  668. -                    *iter = 'a';
  669. +                    *iter = Latin1Char('a');
  670.                      if (++iter == placeholderEnd)
  671.                          return -1;
  672.  
  673. @@ -196,12 +224,12 @@ static int createFileFromTemplate(QByteArray &path, size_t pos, size_t length)
  674.  
  675.                  case '0': case '1': case '2': case '3': case '4':
  676.                  case '5': case '6': case '7': case '8': case '9':
  677. -                    *iter = 'a';
  678. +                    *iter = Latin1Char('a');
  679.                      break;
  680.  
  681.                  case 'z':
  682.                      // increment 'z' to 'A'
  683. -                    *iter = 'A';
  684. +                    *iter = Latin1Char('A');
  685.                      break;
  686.  
  687.                  default:
  688. @@ -314,8 +342,9 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  689.          phLength = 0;
  690.      }
  691.  
  692. -    QByteArray filename;
  693. +    QFileSystemEntry::NativePath filename;
  694.  
  695. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  696.      if (phLength < 6) {
  697.          filename = qfilename.toLocal8Bit();
  698.  
  699. @@ -330,6 +359,16 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  700.          phPos = prefix.length();
  701.          filename = prefix % Placeholder(phLength) % suffix;
  702.      }
  703. +#else
  704. +    if (phLength < 6) {
  705. +        phPos = qfilename.length() + 1; // Account for added dot in prefix
  706. +        phLength = 6;
  707. +        filename = qfilename % Latin1Char('.') % Placeholder(phLength);
  708. +    } else
  709. +        filename = qfilename;
  710. +
  711. +    // No native separators, not a "native path"
  712. +#endif
  713.  
  714.      int fd = createFileFromTemplate(filename, phPos, phLength);
  715.  
  716. @@ -356,7 +395,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  717.      if (fd == -1)
  718.          return false;
  719.  
  720. -    d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename.constData(), filename.length()));
  721. +    d->fileEntry = QFileSystemEntry(filename, QFileSystemEntry::FromInternalPath());
  722.      if (QFSFileEngine::open(openMode)) {
  723.          filePathIsTemplate = false;
  724.          return true;
  725. --
  726. 1.7.5.2
  727.  
  728.  
  729. From 77111a1e945a16ca50c3c4ea60d7a45a8cc1a404 Mon Sep 17 00:00:00 2001
  730. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  731. Date: Fri, 5 Aug 2011 10:45:08 +0200
  732. Subject: [PATCH 10/14] Use native paths on Windows and Symbian
  733.  
  734. On these platforms, we know the native encoding for file names and can
  735. make conversions ahead of time.
  736.  
  737. There's a change in behaviour on Windows and Symbian: fileNames returned
  738. by QTemporaryFile on Windows and Symbian are always absolute after open
  739. has been called. This has to do with how
  740. QFileSystemEntry::nativeFilePath works on these platforms.
  741.  
  742. Test was updated to reflect change in behaviour.
  743.  
  744. Reviewed-by: ?
  745. ---
  746. src/corelib/io/qtemporaryfile.cpp                |   21 +++++++++++---
  747.  tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp |   31 ++++++++++++++--------
  748.  2 files changed, 36 insertions(+), 16 deletions(-)
  749.  
  750. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  751. index e4c53aa..c9d726b 100644
  752. --- a/src/corelib/io/qtemporaryfile.cpp
  753. +++ b/src/corelib/io/qtemporaryfile.cpp
  754. @@ -319,7 +319,14 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  755.      if (!filePathIsTemplate)
  756.          return QFSFileEngine::open(openMode);
  757.  
  758. -    const QString qfilename = d->fileEntry.filePath();
  759. +    const QString qfilename =
  760. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  761. +        // Since the native encoding is out of our control, we need to process
  762. +        // the path as UTF-16 before doing any conversions
  763. +        d->fileEntry.filePath();
  764. +#else
  765. +        d->fileEntry.nativeFilePath();
  766. +#endif
  767.  
  768.      // Find placeholder string.
  769.      uint phPos = qfilename.length();
  770. @@ -333,8 +340,14 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  771.              continue;
  772.          }
  773.  
  774. -        if (qfilename[phPos] == QLatin1Char('/')
  775. -                || phLength >= 6) {
  776. +        if (phLength >= 6
  777. +                || qfilename[phPos] ==
  778. +#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  779. +                    QLatin1Char('/')
  780. +#else
  781. +                    QLatin1Char('\\')
  782. +#endif
  783. +                ) {
  784.              ++phPos;
  785.              break;
  786.          }
  787. @@ -366,8 +379,6 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  788.          filename = qfilename % Latin1Char('.') % Placeholder(phLength);
  789.      } else
  790.          filename = qfilename;
  791. -
  792. -    // No native separators, not a "native path"
  793.  #endif
  794.  
  795.      int fd = createFileFromTemplate(filename, phPos, phLength);
  796. diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  797. index 2edb93a..05fee95 100644
  798. --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  799. +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  800. @@ -201,11 +201,12 @@ void tst_QTemporaryFile::fileTemplate()
  801.  
  802.      QCOMPARE(file.open(), true);
  803.  
  804. +    QString fileName = QFileInfo(file).fileName();
  805.      if (prefix.length())
  806. -        QCOMPARE(file.fileName().left(prefix.length()), prefix);
  807. +        QCOMPARE(fileName.left(prefix.length()), prefix);
  808.  
  809.      if (suffix.length())
  810. -        QCOMPARE(file.fileName().right(suffix.length()), suffix);
  811. +        QCOMPARE(fileName.right(suffix.length()), suffix);
  812.  }
  813.  
  814.  
  815. @@ -701,20 +702,28 @@ void tst_QTemporaryFile::QTBUG_4796()
  816.                  << file5.fileName()
  817.                  << file6.fileName();
  818.  
  819. -            QVERIFY(file1.fileName().startsWith(fileTemplate1 + QLatin1Char('.')));
  820. -            QVERIFY(file2.fileName().startsWith(fileTemplate2 + QLatin1Char('.')));
  821. -            QVERIFY(file5.fileName().startsWith("test-XXXXXX/" + fileTemplate1 + QLatin1Char('.')));
  822. -            QVERIFY(file6.fileName().startsWith("test-XXXXXX/" + prefix));
  823. +            QDir currentDir;
  824. +            QString fileName1 = currentDir.relativeFilePath(file1.fileName());
  825. +            QString fileName2 = currentDir.relativeFilePath(file2.fileName());
  826. +            QString fileName3 = currentDir.relativeFilePath(file3.fileName());
  827. +            QString fileName4 = currentDir.relativeFilePath(file4.fileName());
  828. +            QString fileName5 = currentDir.relativeFilePath(file5.fileName());
  829. +            QString fileName6 = currentDir.relativeFilePath(file6.fileName());
  830. +
  831. +            QVERIFY(fileName1.startsWith(fileTemplate1 + QLatin1Char('.')));
  832. +            QVERIFY(fileName2.startsWith(fileTemplate2 + QLatin1Char('.')));
  833. +            QVERIFY(fileName5.startsWith("test-XXXXXX/" + fileTemplate1 + QLatin1Char('.')));
  834. +            QVERIFY(fileName6.startsWith("test-XXXXXX/" + prefix));
  835.  
  836.              if (!prefix.isEmpty()) {
  837. -                QVERIFY(file3.fileName().startsWith(prefix));
  838. -                QVERIFY(file4.fileName().startsWith(prefix));
  839. +                QVERIFY(fileName3.startsWith(prefix));
  840. +                QVERIFY(fileName4.startsWith(prefix));
  841.              }
  842.  
  843.              if (!suffix.isEmpty()) {
  844. -                QVERIFY(file3.fileName().endsWith(suffix));
  845. -                QVERIFY(file4.fileName().endsWith(suffix));
  846. -                QVERIFY(file6.fileName().endsWith(suffix));
  847. +                QVERIFY(fileName3.endsWith(suffix));
  848. +                QVERIFY(fileName4.endsWith(suffix));
  849. +                QVERIFY(fileName6.endsWith(suffix));
  850.              }
  851.          }
  852.      }
  853. --
  854. 1.7.5.2
  855.  
  856.  
  857. From b78dc17595591096e17265748247aa8463084cd3 Mon Sep 17 00:00:00 2001
  858. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  859. Date: Fri, 5 Aug 2011 10:47:01 +0200
  860. Subject: [PATCH 11/14] Cleanup #includes
  861.  
  862. These are already required and included by qfsfileengine_p.h.
  863. ---
  864. src/corelib/io/qtemporaryfile.cpp |    2 --
  865.  1 files changed, 0 insertions(+), 2 deletions(-)
  866.  
  867. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  868. index c9d726b..ba9a2a7 100644
  869. --- a/src/corelib/io/qtemporaryfile.cpp
  870. +++ b/src/corelib/io/qtemporaryfile.cpp
  871. @@ -44,10 +44,8 @@
  872.  #ifndef QT_NO_TEMPORARYFILE
  873.  
  874.  #include "qplatformdefs.h"
  875. -#include "qabstractfileengine.h"
  876.  #include "qstringbuilder.h"
  877.  #include "private/qfile_p.h"
  878. -#include "private/qabstractfileengine_p.h"
  879.  #include "private/qfsfileengine_p.h"
  880.  
  881.  #if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  882. --
  883. 1.7.5.2
  884.  
  885.  
  886. From 708ea483f465264e80c7a2fcb7c00277cd57f762 Mon Sep 17 00:00:00 2001
  887. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  888. Date: Fri, 5 Aug 2011 11:00:51 +0200
  889. Subject: [PATCH 12/14] Atomic implementation of create file and obtain handle
  890.  for Win/Symbian
  891.  
  892. createFileFromTemplate now not only generates a unique name, but also
  893. acquires a file handle on all platforms. The function directly modifies
  894. the file engine's native handle, returns true on success.
  895.  
  896. Errors, other than "file exists" are propagated up with resulting
  897. failure in opening the temporary file.
  898.  
  899. Fixes a long standing security issue on Windows.
  900.  
  901. This change otherwise unifies error handling and should give consistent
  902. behaviour across all platforms.
  903.  
  904. Reviewed-by: ?
  905. ---
  906. src/corelib/io/qtemporaryfile.cpp |  130 ++++++++++++++++++++++++-------------
  907.  1 files changed, 86 insertions(+), 44 deletions(-)
  908.  
  909. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  910. index ba9a2a7..9f7cde2 100644
  911. --- a/src/corelib/io/qtemporaryfile.cpp
  912. +++ b/src/corelib/io/qtemporaryfile.cpp
  913. @@ -47,6 +47,11 @@
  914.  #include "qstringbuilder.h"
  915.  #include "private/qfile_p.h"
  916.  #include "private/qfsfileengine_p.h"
  917. +#include "private/qsystemerror_p.h"
  918. +
  919. +#if defined(Q_OS_SYMBIAN)
  920. +#include "private/qcore_symbian_p.h"
  921. +#endif
  922.  
  923.  #if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  924.  #include "private/qcore_unix_p.h"       // overrides QT_OPEN
  925. @@ -81,9 +86,20 @@ struct QConcatenable<Char>
  926.      }
  927.  };
  928.  
  929. +# ifdef Q_OS_WIN
  930. +typedef HANDLE NativeFileHandle;
  931. +# else // Q_OS_SYMBIAN
  932. +#  ifdef  SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
  933. +typedef RFile64 NativeFileHandle;
  934. +#  else
  935. +typedef RFile NativeFileHandle;
  936. +#  endif
  937. +# endif
  938. +
  939.  #else // POSIX
  940.  typedef char Char;
  941.  typedef char Latin1Char;
  942. +typedef int NativeFileHandle;
  943.  #endif
  944.  
  945.  struct Placeholder
  946. @@ -159,8 +175,9 @@ struct QConcatenable<Placeholder>
  947.      handle otherwise. In both cases, the string in \a path will be changed and
  948.      contain the generated path name.
  949.  */
  950. -static int createFileFromTemplate(QFileSystemEntry::NativePath &path,
  951. -        size_t pos, size_t length)
  952. +static bool createFileFromTemplate(NativeFileHandle &file,
  953. +        QFileSystemEntry::NativePath &path, size_t pos, size_t length,
  954. +        QSystemError &error)
  955.  {
  956.      Q_ASSERT(length != 0);
  957.      Q_ASSERT(pos < size_t(path.length()));
  958. @@ -190,21 +207,50 @@ static int createFileFromTemplate(QFileSystemEntry::NativePath &path,
  959.          }
  960.      }
  961.  
  962. +#ifdef Q_OS_SYMBIAN
  963. +    RFs& fs = qt_s60GetRFs();
  964. +#endif
  965. +
  966.      for (;;) {
  967.          // Atomically create file and obtain handle
  968. -#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  969. -        {
  970. -            int fd = QT_OPEN(path.constData(),
  971. -                    QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
  972. -                    0600);
  973. -            if (fd != -1)
  974. -                return fd;
  975. -            if (errno != EEXIST)
  976. -                return -1;
  977. +#if defined(Q_OS_WIN)
  978. +        file = CreateFile((const wchar_t *)path.constData(),
  979. +                GENERIC_READ | GENERIC_WRITE,
  980. +                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW,
  981. +                FILE_ATTRIBUTE_NORMAL, NULL);
  982. +
  983. +        if (file != INVALID_HANDLE_VALUE)
  984. +            return true;
  985. +
  986. +        DWORD err = GetLastError();
  987. +        if (err != ERROR_FILE_EXISTS) {
  988. +            error = QSystemError(err, QSystemError::NativeError);
  989. +            return false;
  990. +        }
  991. +#elif defined(Q_OS_SYMBIAN)
  992. +        TInt err = file.Create(fs, qt_QString2TPtrC(path),
  993. +                EFileRead | EFileWrite | EFileShareReadersOrWriters);
  994. +
  995. +        if (err == KErrNone)
  996. +            return true;
  997. +
  998. +        if (err != KErrAlreadyExists) {
  999. +            error = QSystemError(err, QSystemError::NativeError);
  1000. +            return false;
  1001. +        }
  1002. +#else // POSIX
  1003. +        file = QT_OPEN(path.constData(),
  1004. +                QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE,
  1005. +                0600);
  1006. +
  1007. +        if (file != -1)
  1008. +            return true;
  1009. +
  1010. +        int err = errno;
  1011. +        if (err != EEXIST) {
  1012. +            error = QSystemError(err, QSystemError::NativeError);
  1013. +            return false;
  1014.          }
  1015. -#else
  1016. -        if (!QFileInfo(path).exists())
  1017. -            return 1;
  1018.  #endif
  1019.  
  1020.          /* tricky little algorwwithm for backward compatibility */
  1021. @@ -215,8 +261,11 @@ static int createFileFromTemplate(QFileSystemEntry::NativePath &path,
  1022.                  case 'Z':
  1023.                      // Rollover, advance next character
  1024.                      *iter = Latin1Char('a');
  1025. -                    if (++iter == placeholderEnd)
  1026. -                        return -1;
  1027. +                    if (++iter == placeholderEnd) {
  1028. +                        // Out of alternatives. Return file exists error, previously set.
  1029. +                        error = QSystemError(err, QSystemError::NativeError);
  1030. +                        return false;
  1031. +                    }
  1032.  
  1033.                      continue;
  1034.  
  1035. @@ -379,40 +428,33 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1036.          filename = qfilename;
  1037.  #endif
  1038.  
  1039. -    int fd = createFileFromTemplate(filename, phPos, phLength);
  1040. +    QSystemError error;
  1041. +#if defined(Q_OS_WIN)
  1042. +    NativeFileHandle &file = d->fileHandle;
  1043. +#elif defined(Q_OS_SYMBIAN)
  1044. +    NativeFileHandle &file = d->symbianFile;
  1045. +#else // POSIX
  1046. +    NativeFileHandle &file = d->fd;
  1047. +#endif
  1048.  
  1049. -#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  1050. -    if (fd != -1) {
  1051. -        // First open the fd as an external file descriptor to
  1052. -        // initialize the engine properly.
  1053. -        if (QFSFileEngine::open(openMode, fd)) {
  1054. +    if (!createFileFromTemplate(file, filename, phPos, phLength, error)) {
  1055. +        setError(QFile::OpenError, error.toString());
  1056. +        return false;
  1057. +    }
  1058.  
  1059. -            // Allow the engine to close the handle even if it's "external".
  1060. -            d->closeFileHandle = true;
  1061. +    d->fileEntry = QFileSystemEntry(filename, QFileSystemEntry::FromNativePath());
  1062.  
  1063. -            // Restore the file names (open() resets them).
  1064. -            d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename.constData(), filename.length())); //note that filename is NOT a native path
  1065. -            filePathIsTemplate = false;
  1066. -            return true;
  1067. -        }
  1068. +#if !defined(Q_OS_WIN)
  1069. +    d->closeFileHandle = true;
  1070. +#endif
  1071.  
  1072. -        QT_CLOSE(fd);
  1073. -    }
  1074. -    setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  1075. -    return false;
  1076. -#else
  1077. -    if (fd == -1)
  1078. -        return false;
  1079. +    filePathIsTemplate = false;
  1080.  
  1081. -    d->fileEntry = QFileSystemEntry(filename, QFileSystemEntry::FromInternalPath());
  1082. -    if (QFSFileEngine::open(openMode)) {
  1083. -        filePathIsTemplate = false;
  1084. -        return true;
  1085. -    }
  1086. +    d->openMode = openMode;
  1087. +    d->lastFlushFailed = false;
  1088. +    d->tried_stat = 0;
  1089.  
  1090. -    d->fileEntry = QFileSystemEntry(qfilename, QFileSystemEntry::FromInternalPath());
  1091. -    return false;
  1092. -#endif
  1093. +    return true;
  1094.  }
  1095.  
  1096.  bool QTemporaryFileEngine::remove()
  1097. --
  1098. 1.7.5.2
  1099.  
  1100.  
  1101. From a52e6abc5cc254d4f5664ad9e94434f996bd26bf Mon Sep 17 00:00:00 2001
  1102. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1103. Date: Fri, 5 Aug 2011 10:49:44 +0200
  1104. Subject: [PATCH 13/14] Use "native paths" on POSIX platforms as well
  1105.  
  1106. And don't rely solely on "local8Bit" conversions.
  1107.  
  1108. QFile defines an API for overriding how encoding conversions are done
  1109. for filenames. In generating unique names, QTemporaryFile ignored that
  1110. API and hardcoded the use of local 8-bit, implicitly assuming that that
  1111. was appropriate.
  1112.  
  1113. With this change, we switch that assumption to one where user supplied
  1114. encoding function keeps the byte value of 'X' and '/', also assuming
  1115. that encoded 'X' takes up a single-byte (i.e., the byte sequence for
  1116. "XXXXXX" remains unchanged).
  1117.  
  1118. There was also, and there still is an assumption in name generation that
  1119. byte values for ASCII alpha-numeric characters are valid in the "native"
  1120. encoding.
  1121.  
  1122. In practice this change is compatible with UTF-8, Latin-1 and other
  1123. ISO/IEC 8859 encodings. At any rate, it's very likely that only UTF-8 is
  1124. relevant here.
  1125.  
  1126. Reviewed-by: ?
  1127. ---
  1128. src/corelib/io/qtemporaryfile.cpp |   30 +++---------------------------
  1129.  1 files changed, 3 insertions(+), 27 deletions(-)
  1130.  
  1131. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  1132. index 9f7cde2..ac5deac 100644
  1133. --- a/src/corelib/io/qtemporaryfile.cpp
  1134. +++ b/src/corelib/io/qtemporaryfile.cpp
  1135. @@ -366,14 +366,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1136.      if (!filePathIsTemplate)
  1137.          return QFSFileEngine::open(openMode);
  1138.  
  1139. -    const QString qfilename =
  1140. -#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  1141. -        // Since the native encoding is out of our control, we need to process
  1142. -        // the path as UTF-16 before doing any conversions
  1143. -        d->fileEntry.filePath();
  1144. -#else
  1145. -        d->fileEntry.nativeFilePath();
  1146. -#endif
  1147. +    const QFileSystemEntry::NativePath qfilename = d->fileEntry.nativeFilePath();
  1148.  
  1149.      // Find placeholder string.
  1150.      uint phPos = qfilename.length();
  1151. @@ -382,7 +375,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1152.      while (phPos != 0) {
  1153.          --phPos;
  1154.  
  1155. -        if (qfilename[phPos] == QLatin1Char('X')) {
  1156. +        if (qfilename[phPos] == Latin1Char('X')) {
  1157.              ++phLength;
  1158.              continue;
  1159.          }
  1160. @@ -390,7 +383,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1161.          if (phLength >= 6
  1162.                  || qfilename[phPos] ==
  1163.  #if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  1164. -                    QLatin1Char('/')
  1165. +                    '/'
  1166.  #else
  1167.                      QLatin1Char('\\')
  1168.  #endif
  1169. @@ -404,29 +397,12 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1170.  
  1171.      QFileSystemEntry::NativePath filename;
  1172.  
  1173. -#if !defined(Q_OS_WIN) && !defined(Q_OS_SYMBIAN)
  1174. -    if (phLength < 6) {
  1175. -        filename = qfilename.toLocal8Bit();
  1176. -
  1177. -        phPos = filename.length() + 1; // Account for added dot in prefix
  1178. -        phLength = 6;
  1179. -        filename = filename % '.' % Placeholder(phLength);
  1180. -    } else {
  1181. -        QByteArray prefix, suffix;
  1182. -        prefix = qfilename.leftRef(phPos).toLocal8Bit();
  1183. -        suffix = qfilename.midRef(phPos + phLength).toLocal8Bit();
  1184. -
  1185. -        phPos = prefix.length();
  1186. -        filename = prefix % Placeholder(phLength) % suffix;
  1187. -    }
  1188. -#else
  1189.      if (phLength < 6) {
  1190.          phPos = qfilename.length() + 1; // Account for added dot in prefix
  1191.          phLength = 6;
  1192.          filename = qfilename % Latin1Char('.') % Placeholder(phLength);
  1193.      } else
  1194.          filename = qfilename;
  1195. -#endif
  1196.  
  1197.      QSystemError error;
  1198.  #if defined(Q_OS_WIN)
  1199. --
  1200. 1.7.5.2
  1201.  
  1202.  
  1203. From bdc307bfa48f921c8dcc1b4ac510cfcda30f5f6c Mon Sep 17 00:00:00 2001
  1204. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1205. Date: Wed, 10 Aug 2011 18:11:25 +0200
  1206. Subject: [PATCH 14/14] Fix warning when compiling with VS 2008
  1207.  
  1208. warning C4308: negative integral constant converted to unsigned type
  1209. ---
  1210. src/corelib/tools/qbytearray.cpp |    4 ++--
  1211.  1 files changed, 2 insertions(+), 2 deletions(-)
  1212.  
  1213. diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
  1214. index 767b713..a8edea4 100644
  1215. --- a/src/corelib/tools/qbytearray.cpp
  1216. +++ b/src/corelib/tools/qbytearray.cpp
  1217. @@ -541,7 +541,7 @@ QByteArray qUncompress(const uchar* data, int nbytes)
  1218.  
  1219.      forever {
  1220.          ulong alloc = len;
  1221. -        if (len  >= (1 << 31) - sizeof(QByteArray::Data)) {
  1222. +        if (len  >= ulong(1 << 31) - sizeof(QByteArray::Data)) {
  1223.              //QByteArray does not support that huge size anyway.
  1224.              qWarning("qUncompress: Input data is corrupted");
  1225.              return QByteArray();
  1226. @@ -561,7 +561,7 @@ QByteArray qUncompress(const uchar* data, int nbytes)
  1227.          switch (res) {
  1228.          case Z_OK:
  1229.              if (len != alloc) {
  1230. -                if (len  >= (1 << 31) - sizeof(QByteArray::Data)) {
  1231. +                if (len  >= ulong(1 << 31) - sizeof(QByteArray::Data)) {
  1232.                      //QByteArray does not support that huge size anyway.
  1233.                      qWarning("qUncompress: Input data is corrupted");
  1234.                      return QByteArray();
  1235. --
  1236. 1.7.5.2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement