Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 41.17 KB | None | 0 0
  1. From d5686dae1e4acdc5ea51d025dd25e337a1028f2a Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  3. Date: Mon, 14 Feb 2011 17:55:29 +0100
  4. Subject: [PATCH 01/14] Test should fail, debug output here is useless
  5.  
  6. ---
  7. tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp |    5 +----
  8.  1 files changed, 1 insertions(+), 4 deletions(-)
  9.  
  10. diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  11. index f89e033..0ae3271 100644
  12. --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  13. +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  14. @@ -359,10 +359,7 @@ void tst_QTemporaryFile::stressTest()
  15.      for (int i = 0; i < iterations; ++i) {
  16.          QTemporaryFile file;
  17.          file.setAutoRemove(false);
  18. -        if (!file.open()) {
  19. -            qDebug() << "Could not open File:" << file.fileName();
  20. -            continue;
  21. -        }
  22. +        QVERIFY(file.open());
  23.          QVERIFY(!names.contains(file.fileName()));
  24.          names.insert(file.fileName());
  25.      }
  26. --
  27. 1.7.3.4
  28.  
  29.  
  30. From 1ae062818c7577c24a686980621125e86c5df8b1 Mon Sep 17 00:00:00 2001
  31. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  32. Date: Mon, 14 Feb 2011 17:57:04 +0100
  33. Subject: [PATCH 02/14] Adding unicode test case for QTemporaryFile
  34.  
  35. Task-number: QTBUG-4796
  36. Reviewed-by: ?
  37. ---
  38. tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp |  122 ++++++++++++++++++++++
  39.  1 files changed, 122 insertions(+), 0 deletions(-)
  40.  
  41. diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  42. index 0ae3271..995107a 100644
  43. --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  44. +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  45. @@ -75,6 +75,10 @@ public:
  46.  public slots:
  47.      void init();
  48.      void cleanup();
  49. +
  50. +    void initTestCase();
  51. +    void cleanupTestCase();
  52. +
  53.  private slots:
  54.      void construction();
  55.      void fileTemplate();
  56. @@ -97,9 +101,24 @@ private slots:
  57.      void setTemplateAfterOpen();
  58.      void autoRemoveAfterFailedRename();
  59.  
  60. +    void QTBUG_4796_data();
  61. +    void QTBUG_4796();
  62. +
  63.  public:
  64.  };
  65.  
  66. +void tst_QTemporaryFile::initTestCase()
  67. +{
  68. +    // For QTBUG_4796
  69. +    QVERIFY(QDir("test-XXXXXX").exists() || QDir().mkdir("test-XXXXXX"));
  70. +}
  71. +
  72. +void tst_QTemporaryFile::cleanupTestCase()
  73. +{
  74. +    // From QTBUG_4796
  75. +    QVERIFY(QDir().rmdir("test-XXXXXX"));
  76. +}
  77. +
  78.  void tst_QTemporaryFile::construction()
  79.  {
  80.      QTemporaryFile file(0);
  81. @@ -591,5 +610,108 @@ void tst_QTemporaryFile::autoRemoveAfterFailedRename()
  82.      cleaner.reset();
  83.  }
  84.  
  85. +void tst_QTemporaryFile::QTBUG_4796_data()
  86. +{
  87. +    QTest::addColumn<QString>("prefix");
  88. +    QTest::addColumn<QString>("suffix");
  89. +    QTest::addColumn<bool>("openResult");
  90. +
  91. +    QString unicode = QString::fromUtf8("\xc3\xa5\xc3\xa6\xc3\xb8");
  92. +
  93. +    QTest::newRow("<empty>") << QString() << QString() << true;
  94. +    QTest::newRow("blaXXXXXX") << QString("bla") << QString() << true;
  95. +    QTest::newRow("XXXXXXbla") << QString() << QString("bla") << true;
  96. +    QTest::newRow("does-not-exist/qt_temp.XXXXXX") << QString("does-not-exist/qt_temp") << QString() << false;
  97. +    QTest::newRow("XXXXXX<unicode>") << QString() << unicode << true;
  98. +    QTest::newRow("<unicode>XXXXXX") << unicode << QString() << true;
  99. +    QTest::newRow("<unicode>XXXXXX<unicode>") << unicode << unicode << true;
  100. +}
  101. +
  102. +void tst_QTemporaryFile::QTBUG_4796()
  103. +{
  104. +    QVERIFY(QDir("test-XXXXXX").exists());
  105. +
  106. +    struct CleanOnReturn
  107. +    {
  108. +        ~CleanOnReturn()
  109. +        {
  110. +            Q_FOREACH(QString tempName, tempNames)
  111. +                QFile::remove(tempName);
  112. +        }
  113. +
  114. +        void reset()
  115. +        {
  116. +            tempNames.clear();
  117. +        }
  118. +
  119. +        QStringList tempNames;
  120. +    };
  121. +
  122. +    CleanOnReturn cleaner;
  123. +
  124. +    QFETCH(QString, prefix);
  125. +    QFETCH(QString, suffix);
  126. +    QFETCH(bool, openResult);
  127. +
  128. +    {
  129. +        QString fileTemplate1 = prefix + QString("XX") + suffix;
  130. +        QString fileTemplate2 = prefix + QString("XXXX") + suffix;
  131. +        QString fileTemplate3 = prefix + QString("XXXXXX") + suffix;
  132. +        QString fileTemplate4 = prefix + QString("XXXXXXXX") + suffix;
  133. +
  134. +        QTemporaryFile file1(fileTemplate1);
  135. +        QTemporaryFile file2(fileTemplate2);
  136. +        QTemporaryFile file3(fileTemplate3);
  137. +        QTemporaryFile file4(fileTemplate4);
  138. +        QTemporaryFile file5("test-XXXXXX/" + fileTemplate1);
  139. +        QTemporaryFile file6("test-XXXXXX/" + fileTemplate3);
  140. +
  141. +        QCOMPARE(file1.open(), openResult);
  142. +        QCOMPARE(file2.open(), openResult);
  143. +        QCOMPARE(file3.open(), openResult);
  144. +        QCOMPARE(file4.open(), openResult);
  145. +        QCOMPARE(file5.open(), openResult);
  146. +        QCOMPARE(file6.open(), openResult);
  147. +
  148. +        QCOMPARE(file1.exists(), openResult);
  149. +        QCOMPARE(file2.exists(), openResult);
  150. +        QCOMPARE(file3.exists(), openResult);
  151. +        QCOMPARE(file4.exists(), openResult);
  152. +        QCOMPARE(file5.exists(), openResult);
  153. +        QCOMPARE(file6.exists(), openResult);
  154. +
  155. +        // make sure the file exists under the *correct* name
  156. +        if (openResult) {
  157. +            cleaner.tempNames << file1.fileName()
  158. +                << file2.fileName()
  159. +                << file3.fileName()
  160. +                << file4.fileName()
  161. +                << file5.fileName()
  162. +                << file6.fileName();
  163. +
  164. +            QVERIFY(file1.fileName().startsWith(fileTemplate1 + QLatin1Char('.')));
  165. +            QVERIFY(file2.fileName().startsWith(fileTemplate2 + QLatin1Char('.')));
  166. +            QVERIFY(file5.fileName().startsWith("test-XXXXXX/" + fileTemplate1 + QLatin1Char('.')));
  167. +            QVERIFY(file6.fileName().startsWith("test-XXXXXX/" + prefix));
  168. +
  169. +            if (!prefix.isEmpty()) {
  170. +                QVERIFY(file3.fileName().startsWith(prefix));
  171. +                QVERIFY(file4.fileName().startsWith(prefix));
  172. +            }
  173. +
  174. +            if (!suffix.isEmpty()) {
  175. +                QVERIFY(file3.fileName().endsWith(suffix));
  176. +                QVERIFY(file4.fileName().endsWith(suffix));
  177. +                QVERIFY(file6.fileName().endsWith(suffix));
  178. +            }
  179. +        }
  180. +    }
  181. +
  182. +    Q_FOREACH(QString tempName, cleaner.tempNames)
  183. +        QVERIFY( !QFile::exists(tempName) );
  184. +
  185. +    cleaner.reset();
  186. +}
  187. +
  188.  QTEST_MAIN(tst_QTemporaryFile)
  189.  #include "tst_qtemporaryfile.moc"
  190. --
  191. 1.7.3.4
  192.  
  193.  
  194. From c14e04ef22147f972908b74d6e63a896b9dc9450 Mon Sep 17 00:00:00 2001
  195. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  196. Date: Wed, 30 Mar 2011 14:11:33 +0200
  197. Subject: [PATCH 03/14] Extending fileTemplate autotest
  198.  
  199. ... to also verify template prefix and corner cases with runs of less
  200. than 6 Xs.
  201.  
  202. Reviewed-by: ?
  203. ---
  204. tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp |   40 ++++++++++++++--------
  205.  1 files changed, 26 insertions(+), 14 deletions(-)
  206.  
  207. diff --git a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  208. index 995107a..4e36301 100644
  209. --- a/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  210. +++ b/tests/auto/qtemporaryfile/tst_qtemporaryfile.cpp
  211. @@ -163,26 +163,35 @@ void tst_QTemporaryFile::cleanup()
  212.  void tst_QTemporaryFile::fileTemplate_data()
  213.  {
  214.      QTest::addColumn<QString>("constructorTemplate");
  215. +    QTest::addColumn<QString>("prefix");
  216.      QTest::addColumn<QString>("suffix");
  217.      QTest::addColumn<QString>("fileTemplate");
  218.  
  219. -    QTest::newRow("constructor default")          << "" << "" << "";
  220. -    QTest::newRow("constructor with xxx sufix") << "qt_XXXXXXxxx" << "xxx" << "";
  221. -    QTest::newRow("constructor with xXx sufix") << "qt_XXXXXXxXx" << "xXx" << "";
  222. -    QTest::newRow("constructor with no sufix") << "qt_XXXXXX" << "" << "";
  223. -    QTest::newRow("constructor with >6 X's and xxx suffix") << "qt_XXXXXXXXXXxxx" << "xxx" << "";
  224. -    QTest::newRow("constructor with >6 X's, no suffix") << "qt_XXXXXXXXXX" << "" << "";
  225. -
  226. -    QTest::newRow("set template, no suffix") << "" << "" << "foo";
  227. -    QTest::newRow("set template, with lowercase XXXXXX") << "" << "xxxxxx" << "qt_XXXXXXxxxxxx";
  228. -    QTest::newRow("set template, with xxx") << "" << ".xxx" << "qt_XXXXXX.xxx";
  229. -    QTest::newRow("set template, with >6 X's") << "" << ".xxx" << "qt_XXXXXXXXXXXXXX.xxx";
  230. -    QTest::newRow("set template, with >6 X's, no suffix") << "" << "" << "qt_XXXXXXXXXXXXXX";
  231. +    QTest::newRow("constructor default") << "" << "." << "" << "";
  232. +    QTest::newRow("constructor with xxx sufix") << "qt_XXXXXXxxx" << "qt_" << "xxx" << "";
  233. +    QTest::newRow("constructor with xXx sufix") << "qt_XXXXXXxXx" << "qt_" << "xXx" << "";
  234. +    QTest::newRow("constructor with no sufix") << "qt_XXXXXX" << "qt_" << "" << "";
  235. +    QTest::newRow("constructor with >6 X's and xxx suffix") << "qt_XXXXXXXXXXxxx" << "qt_" << "xxx" << "";
  236. +    QTest::newRow("constructor with >6 X's, no suffix") << "qt_XXXXXXXXXX" << "qt_" << "" << "";
  237. +
  238. +    QTest::newRow("constructor with XXXX suffix") << "qt_XXXXXX_XXXX" << "qt_" << "_XXXX" << "";
  239. +    QTest::newRow("constructor with XXXXX suffix") << "qt_XXXXXX_XXXXX" << "qt_" << "_XXXXX" << "";
  240. +    QTest::newRow("constructor with XXXX prefix") << "qt_XXXX" << "qt_XXXX." << "" << "";
  241. +    QTest::newRow("constructor with XXXXX prefix") << "qt_XXXXX" << "qt_XXXXX." << "" << "";
  242. +    QTest::newRow("constructor with XXXX  prefix and suffix") << "qt_XXXX_XXXXXX_XXXX" << "qt_XXXX_" << "_XXXX" << "";
  243. +    QTest::newRow("constructor with XXXXX prefix and suffix") << "qt_XXXXX_XXXXXX_XXXXX" << "qt_XXXXX_" << "_XXXXX" << "";
  244. +
  245. +    QTest::newRow("set template, no suffix") << "" << "foo" << "" << "foo";
  246. +    QTest::newRow("set template, with lowercase XXXXXX") << "" << "qt_" << "xxxxxx" << "qt_XXXXXXxxxxxx";
  247. +    QTest::newRow("set template, with xxx") << "" << "qt_" << ".xxx" << "qt_XXXXXX.xxx";
  248. +    QTest::newRow("set template, with >6 X's") << "" << "qt_" << ".xxx" << "qt_XXXXXXXXXXXXXX.xxx";
  249. +    QTest::newRow("set template, with >6 X's, no suffix") << "" << "qt_" << "" << "qt_XXXXXXXXXXXXXX";
  250.  }
  251.  
  252.  void tst_QTemporaryFile::fileTemplate()
  253.  {
  254.      QFETCH(QString, constructorTemplate);
  255. +    QFETCH(QString, prefix);
  256.      QFETCH(QString, suffix);
  257.      QFETCH(QString, fileTemplate);
  258.  
  259. @@ -192,8 +201,11 @@ void tst_QTemporaryFile::fileTemplate()
  260.  
  261.      QCOMPARE(file.open(), true);
  262.  
  263. -    QCOMPARE(file.fileName().right(suffix.length()), suffix);
  264. -    file.close();
  265. +    if (prefix.length())
  266. +        QCOMPARE(file.fileName().left(prefix.length()), prefix);
  267. +
  268. +    if (suffix.length())
  269. +        QCOMPARE(file.fileName().right(suffix.length()), suffix);
  270.  }
  271.  
  272.  
  273. --
  274. 1.7.3.4
  275.  
  276.  
  277. From db8c0140a28ad4bf11f3225551419b2bc565cd85 Mon Sep 17 00:00:00 2001
  278. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  279. Date: Fri, 25 Mar 2011 17:11:53 +0100
  280. Subject: [PATCH 04/14] Remove duplicate #include
  281.  
  282. ---
  283. src/corelib/io/qtemporaryfile.cpp |    1 -
  284.  1 files changed, 0 insertions(+), 1 deletions(-)
  285.  
  286. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  287. index 56ed746..1a07d08 100644
  288. --- a/src/corelib/io/qtemporaryfile.cpp
  289. +++ b/src/corelib/io/qtemporaryfile.cpp
  290. @@ -49,7 +49,6 @@
  291.  #include "private/qabstractfileengine_p.h"
  292.  #include "private/qfsfileengine_p.h"
  293.  
  294. -#include <stdlib.h>
  295.  #if !defined(Q_OS_WINCE)
  296.  #  include <errno.h>
  297.  #  include <sys/stat.h>
  298. --
  299. 1.7.3.4
  300.  
  301.  
  302. From 0f7bd4898a67ff0cdf32b290dfed8bb826041f2c Mon Sep 17 00:00:00 2001
  303. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  304. Date: Wed, 10 Nov 2010 11:32:19 +0100
  305. Subject: [PATCH 05/14] Removing dead code in QTemporaryFile
  306.  
  307. The option to create a directory is never used, so let's drop it. There
  308. are bigger issues with this code.
  309.  
  310. Reviewed-by: ? pending
  311. ---
  312. src/corelib/io/qtemporaryfile.cpp |   22 ++++------------------
  313.  1 files changed, 4 insertions(+), 18 deletions(-)
  314.  
  315. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  316. index 1a07d08..835f24f 100644
  317. --- a/src/corelib/io/qtemporaryfile.cpp
  318. +++ b/src/corelib/io/qtemporaryfile.cpp
  319. @@ -108,7 +108,7 @@ QT_BEGIN_NAMESPACE
  320.   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  321.   * SUCH DAMAGE.
  322.   */
  323. -static int _gettemp(char *path, int *doopen, int domkdir, int slen)
  324. +static int _gettemp(char *path, int *doopen, int slen)
  325.  {
  326.      char *start, *trv, *suffp;
  327.      QT_STATBUF sbuf;
  328. @@ -119,11 +119,6 @@ static int _gettemp(char *path, int *doopen, int domkdir, int slen)
  329.      pid_t pid;
  330.  #endif
  331.  
  332. -    if (doopen && domkdir) {
  333. -        errno = EINVAL;
  334. -        return 0;
  335. -    }
  336. -
  337.      for (trv = path; *trv; ++trv)
  338.          ;
  339.      trv -= slen;
  340. @@ -166,7 +161,7 @@ static int _gettemp(char *path, int *doopen, int domkdir, int slen)
  341.       * check the target directory; if you have six X's and it
  342.       * doesn't exist this runs for a *very* long time.
  343.       */
  344. -    if (doopen || domkdir) {
  345. +    if (doopen) {
  346.          for (;; --trv) {
  347.              if (trv <= path)
  348.                  break;
  349. @@ -234,15 +229,6 @@ static int _gettemp(char *path, int *doopen, int domkdir, int slen)
  350.              }
  351.              if (errno != EEXIST)
  352.                  return 0;
  353. -        } else if (domkdir) {
  354. -#ifdef Q_OS_WIN
  355. -            if (QT_MKDIR(path) == 0)
  356. -#else
  357. -            if (QT_MKDIR(path, 0700) == 0)
  358. -#endif
  359. -                return 1;
  360. -            if (errno != EEXIST)
  361. -                return 0;
  362.          }
  363.  #ifndef Q_OS_WIN
  364.          else if (QT_LSTAT(path, &sbuf))
  365. @@ -281,7 +267,7 @@ static int _gettemp(char *path, int *doopen, int domkdir, int slen)
  366.  static int qt_mkstemps(char *path, int slen)
  367.  {
  368.      int fd = 0;
  369. -    return (_gettemp(path, &fd, 0, slen) ? fd : -1);
  370. +    return (_gettemp(path, &fd, slen) ? fd : -1);
  371.  }
  372.  #endif
  373.  
  374. @@ -391,7 +377,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  375.      setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  376.      return false;
  377.  #else
  378. -    if (!_gettemp(filename, 0, 0, suffixLength)) {
  379. +    if (!_gettemp(filename, 0, suffixLength)) {
  380.          delete [] filename;
  381.          return false;
  382.      }
  383. --
  384. 1.7.3.4
  385.  
  386.  
  387. From 890258bd43be1cc9a559d694dd067816ddb2971c Mon Sep 17 00:00:00 2001
  388. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  389. Date: Thu, 11 Nov 2010 15:22:49 +0100
  390. Subject: [PATCH 06/14] QTemporaryFile: More dead code elimination
  391.  
  392. There are exactly 2 uses of the _gettemp function: one for windows
  393. platforms, one for all others. Windows requests a file name, others
  394. request a valid file descriptor for that file name.
  395.  
  396. Despite the 2 distinct use cases, the code was full of #ifdefs to keep
  397. never used code paths compiling.
  398.  
  399. With this change the qt_mkstemp wrapper is eliminated, together with
  400. dead code inside if (doopen) {} blocks on Windows platforms. The more
  401. proper Q_OS_WIN macro is preferred to Q_WS_WIN.
  402.  
  403. _gettemp is also changed to return the valid file descriptor on
  404. non-Windows platforms.
  405.  
  406. Reviewed-by: ? pending
  407. ---
  408. src/corelib/io/qtemporaryfile.cpp |  113 ++++++++++---------------------------
  409.  1 files changed, 29 insertions(+), 84 deletions(-)
  410.  
  411. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  412. index 835f24f..bb38906 100644
  413. --- a/src/corelib/io/qtemporaryfile.cpp
  414. +++ b/src/corelib/io/qtemporaryfile.cpp
  415. @@ -65,9 +65,6 @@
  416.  
  417.  #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  418.  # include <process.h>
  419. -# if defined(_MSC_VER) && _MSC_VER >= 1400
  420. -#  include <share.h>
  421. -# endif
  422.  #endif
  423.  
  424.  #if defined(Q_OS_WINCE)
  425. @@ -108,7 +105,7 @@ QT_BEGIN_NAMESPACE
  426.   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  427.   * SUCH DAMAGE.
  428.   */
  429. -static int _gettemp(char *path, int *doopen, int slen)
  430. +static int _gettemp(char *path, int slen)
  431.  {
  432.      char *start, *trv, *suffp;
  433.      QT_STATBUF sbuf;
  434. @@ -126,7 +123,7 @@ static int _gettemp(char *path, int *doopen, int slen)
  435.      --trv;
  436.      if (trv < path) {
  437.          errno = EINVAL;
  438. -        return 0;
  439. +        return -1;
  440.      }
  441.  #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
  442.      pid = _getpid();
  443. @@ -157,82 +154,38 @@ static int _gettemp(char *path, int *doopen, int slen)
  444.      }
  445.      start = trv + 1;
  446.  
  447. +#ifndef Q_OS_WIN
  448.      /*
  449.       * check the target directory; if you have six X's and it
  450.       * doesn't exist this runs for a *very* long time.
  451.       */
  452. -    if (doopen) {
  453. -        for (;; --trv) {
  454. -            if (trv <= path)
  455. -                break;
  456. -            if (*trv == '/') {
  457. -                *trv = '\0';
  458. -#if defined (Q_OS_WIN) && !defined(Q_OS_WINCE)
  459. -                if (trv - path == 2 && path[1] == ':') {
  460. -                    // Special case for Windows drives
  461. -                    // (e.g., "C:" => "C:\").
  462. -                    // ### Better to use a Windows
  463. -                    // call for this.
  464. -                    char drive[] = "c:\\";
  465. -                    drive[0] = path[0];
  466. -                    rval = QT_STAT(drive, &sbuf);
  467. -                } else
  468. -#endif
  469. -                    rval = QT_STAT(path, &sbuf);
  470. -                *trv = '/';
  471. -                if (rval != 0)
  472. -                    return 0;
  473. -                if (!S_ISDIR(sbuf.st_mode)) {
  474. -                    errno = ENOTDIR;
  475. -                    return 0;
  476. -                }
  477. -                break;
  478. +    for (;; --trv) {
  479. +        if (trv <= path)
  480. +            break;
  481. +        if (*trv == '/') {
  482. +            *trv = '\0';
  483. +            rval = QT_STAT(path, &sbuf);
  484. +            *trv = '/';
  485. +            if (rval != 0)
  486. +                return -1;
  487. +            if (!S_ISDIR(sbuf.st_mode)) {
  488. +                errno = ENOTDIR;
  489. +                return -1;
  490.              }
  491. +            break;
  492.          }
  493.      }
  494. +#endif
  495.  
  496.      for (;;) {
  497. -        if (doopen) {
  498. -#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && defined(_MSC_VER) && _MSC_VER >= 1400
  499. -            if (_sopen_s(doopen, path, QT_OPEN_CREAT|O_EXCL|QT_OPEN_RDWR|QT_OPEN_BINARY
  500. -#  ifdef QT_LARGEFILE_SUPPORT
  501. -                                       |QT_OPEN_LARGEFILE
  502. -#  endif
  503. -                         , _SH_DENYNO, _S_IREAD | _S_IWRITE)== 0)
  504. -#else // WIN && !CE
  505. -#  if defined(Q_OS_WINCE)
  506. -            QString targetPath;
  507. -            if (QDir::isAbsolutePath(QString::fromLatin1(path)))
  508. -                targetPath = QLatin1String(path);
  509. -            else
  510. -                targetPath = QDir::currentPath().append(QLatin1Char('/')) + QLatin1String(path);
  511. -
  512. -            if ((*doopen =
  513. -                QT_OPEN(targetPath.toLocal8Bit(), O_CREAT|O_EXCL|O_RDWR
  514. -#  else // CE
  515. -            // this is Unix or older MSVC
  516. -            if ((*doopen =
  517. -                QT_OPEN(path, QT_OPEN_CREAT|O_EXCL|QT_OPEN_RDWR
  518. -#  endif
  519. -#  ifdef QT_LARGEFILE_SUPPORT
  520. -                           |QT_OPEN_LARGEFILE
  521. -#  endif
  522. -#  if defined(Q_OS_WINCE)
  523. -                           |_O_BINARY
  524. -#  elif defined(Q_OS_WIN)
  525. -                           |O_BINARY
  526. -#  endif
  527. -                     , 0600)) >= 0)
  528. -#endif // WIN && !CE
  529. -            {
  530. -                return 1;
  531. -            }
  532. +#ifndef Q_OS_WIN
  533. +        {
  534. +            int fd = QT_OPEN(path, QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE, 0600);
  535. +            if (fd != -1)
  536. +                return fd;
  537.              if (errno != EEXIST)
  538. -                return 0;
  539. +                return -1;
  540.          }
  541. -#ifndef Q_OS_WIN
  542. -        else if (QT_LSTAT(path, &sbuf))
  543. -            return (errno == ENOENT) ? 1 : 0;
  544.  #else
  545.          if (!QFileInfo(QLatin1String(path)).exists())
  546.              return 1;
  547. @@ -241,10 +194,10 @@ static int _gettemp(char *path, int *doopen, int slen)
  548.          /* tricky little algorwwithm for backward compatibility */
  549.          for (trv = start;;) {
  550.              if (!*trv)
  551. -                return 0;
  552. +                return -1;
  553.              if (*trv == 'Z') {
  554.                  if (trv == suffp)
  555. -                    return 0;
  556. +                    return -1;
  557.                  *trv++ = 'a';
  558.              } else {
  559.                  if (isdigit(*trv))
  560. @@ -253,7 +206,7 @@ static int _gettemp(char *path, int *doopen, int slen)
  561.                      *trv = 'A';
  562.                  else {
  563.                      if (trv == suffp)
  564. -                        return 0;
  565. +                        return -1;
  566.                      ++*trv;
  567.                  }
  568.                  break;
  569. @@ -263,14 +216,6 @@ static int _gettemp(char *path, int *doopen, int slen)
  570.      /*NOTREACHED*/
  571.  }
  572.  
  573. -#ifndef Q_WS_WIN
  574. -static int qt_mkstemps(char *path, int slen)
  575. -{
  576. -    int fd = 0;
  577. -    return (_gettemp(path, &fd, slen) ? fd : -1);
  578. -}
  579. -#endif
  580. -
  581.  //************* QTemporaryFileEngine
  582.  class QTemporaryFileEngine : public QFSFileEngine
  583.  {
  584. @@ -354,8 +299,8 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  585.      int suffixLength = qfilename.length() - (qfilename.lastIndexOf(QLatin1String("XXXXXX"), -1, Qt::CaseSensitive) + 6);
  586.      char *filename = qstrdup(qfilename.toLocal8Bit());
  587.  
  588. -#ifndef Q_WS_WIN
  589. -    int fd = qt_mkstemps(filename, suffixLength);
  590. +#ifndef Q_OS_WIN
  591. +    int fd = _gettemp(filename, suffixLength);
  592.      if (fd != -1) {
  593.          // First open the fd as an external file descriptor to
  594.          // initialize the engine properly.
  595. @@ -377,7 +322,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  596.      setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  597.      return false;
  598.  #else
  599. -    if (!_gettemp(filename, 0, suffixLength)) {
  600. +    if (_gettemp(filename, suffixLength) == -1) {
  601.          delete [] filename;
  602.          return false;
  603.      }
  604. --
  605. 1.7.3.4
  606.  
  607.  
  608. From c4e6fa24b20921a0b1bfeda3fe37da52ffeb2163 Mon Sep 17 00:00:00 2001
  609. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  610. Date: Fri, 25 Mar 2011 18:13:04 +0100
  611. Subject: [PATCH 07/14] Don't pre-check if directory exists as it's unnecessary
  612.  
  613. On Posix platforms, when attempting to atomically create file and obtain
  614. descriptor, we only continue if we get an EEXIST error. If the path
  615. points to a non-existing directory, we should get ENOENT and will fail
  616. as well.
  617.  
  618. On Windows, the check was already ignored. We would detect the file does
  619. not exist, attempt to obtain a handle upon return to
  620. QTemporaryFileEngine::open and fail there without running "for a *very*
  621. long time".
  622.  
  623. Checking if the directory exists beforehand is not only unnecessary it
  624. also constitutes a race condition. Trusting the result of this check is
  625. dangerous and doubly useless.
  626.  
  627. Reviewed-by: ?
  628. ---
  629. src/corelib/io/qtemporaryfile.cpp |   35 -----------------------------------
  630.  1 files changed, 0 insertions(+), 35 deletions(-)
  631.  
  632. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  633. index bb38906..a9bed8b 100644
  634. --- a/src/corelib/io/qtemporaryfile.cpp
  635. +++ b/src/corelib/io/qtemporaryfile.cpp
  636. @@ -51,8 +51,6 @@
  637.  
  638.  #if !defined(Q_OS_WINCE)
  639.  #  include <errno.h>
  640. -#  include <sys/stat.h>
  641. -#  include <sys/types.h>
  642.  #endif
  643.  
  644.  #include <stdlib.h>
  645. @@ -67,10 +65,6 @@
  646.  # include <process.h>
  647.  #endif
  648.  
  649. -#if defined(Q_OS_WINCE)
  650. -#  include <types.h>
  651. -#endif
  652. -
  653.  #if defined(Q_OS_VXWORKS)
  654.  #  include <taskLib.h>
  655.  #endif
  656. @@ -108,8 +102,6 @@ QT_BEGIN_NAMESPACE
  657.  static int _gettemp(char *path, int slen)
  658.  {
  659.      char *start, *trv, *suffp;
  660. -    QT_STATBUF sbuf;
  661. -    int rval;
  662.  #if defined(Q_OS_WIN)
  663.      int pid;
  664.  #else
  665. @@ -137,10 +129,6 @@ static int _gettemp(char *path, int slen)
  666.          pid /= 10;
  667.      }
  668.  
  669. -#ifndef S_ISDIR
  670. -#  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  671. -#endif
  672. -
  673.      while (trv >= path && *trv == 'X') {
  674.          char c;
  675.  
  676. @@ -154,29 +142,6 @@ static int _gettemp(char *path, int slen)
  677.      }
  678.      start = trv + 1;
  679.  
  680. -#ifndef Q_OS_WIN
  681. -    /*
  682. -     * check the target directory; if you have six X's and it
  683. -     * doesn't exist this runs for a *very* long time.
  684. -     */
  685. -    for (;; --trv) {
  686. -        if (trv <= path)
  687. -            break;
  688. -        if (*trv == '/') {
  689. -            *trv = '\0';
  690. -            rval = QT_STAT(path, &sbuf);
  691. -            *trv = '/';
  692. -            if (rval != 0)
  693. -                return -1;
  694. -            if (!S_ISDIR(sbuf.st_mode)) {
  695. -                errno = ENOTDIR;
  696. -                return -1;
  697. -            }
  698. -            break;
  699. -        }
  700. -    }
  701. -#endif
  702. -
  703.      for (;;) {
  704.  #ifndef Q_OS_WIN
  705.          {
  706. --
  707. 1.7.3.4
  708.  
  709.  
  710. From 5c1d3a05fb32711261e5e57a58ac3805a3b9a3b4 Mon Sep 17 00:00:00 2001
  711. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  712. Date: Mon, 28 Mar 2011 13:42:58 +0200
  713. Subject: [PATCH 08/14] Adding comments; variable scoping
  714.  
  715. There should be no semantic differences, otherwise.
  716.  
  717. Reviewed-by: ?
  718. ---
  719. src/corelib/io/qtemporaryfile.cpp |   52 +++++++++++++++++++++---------------
  720.  1 files changed, 30 insertions(+), 22 deletions(-)
  721.  
  722. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  723. index a9bed8b..939ea34 100644
  724. --- a/src/corelib/io/qtemporaryfile.cpp
  725. +++ b/src/corelib/io/qtemporaryfile.cpp
  726. @@ -102,11 +102,6 @@ QT_BEGIN_NAMESPACE
  727.  static int _gettemp(char *path, int slen)
  728.  {
  729.      char *start, *trv, *suffp;
  730. -#if defined(Q_OS_WIN)
  731. -    int pid;
  732. -#else
  733. -    pid_t pid;
  734. -#endif
  735.  
  736.      for (trv = path; *trv; ++trv)
  737.          ;
  738. @@ -117,32 +112,43 @@ static int _gettemp(char *path, int slen)
  739.          errno = EINVAL;
  740.          return -1;
  741.      }
  742. +
  743. +    // Initialize placeholder with random chars + PID.
  744. +    {
  745. +#if defined(Q_OS_WIN)
  746. +        int pid;
  747. +#else
  748. +        pid_t pid;
  749. +#endif
  750. +
  751.  #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
  752. -    pid = _getpid();
  753. +        pid = _getpid();
  754.  #elif defined(Q_OS_VXWORKS)
  755. -    pid = (pid_t) taskIdCurrent;
  756. +        pid = (pid_t) taskIdCurrent;
  757.  #else
  758. -    pid = getpid();
  759. +        pid = getpid();
  760.  #endif
  761. -    while (trv >= path && *trv == 'X' && pid != 0) {
  762. -        *trv-- = (pid % 10) + '0';
  763. -        pid /= 10;
  764. -    }
  765. +        while (trv >= path && *trv == 'X' && pid != 0) {
  766. +            *trv-- = (pid % 10) + '0';
  767. +            pid /= 10;
  768. +        }
  769.  
  770. -    while (trv >= path && *trv == 'X') {
  771. -        char c;
  772. +        while (trv >= path && *trv == 'X') {
  773. +            char c;
  774.  
  775. -        // CHANGE arc4random() -> random()
  776. -        pid = (qrand() & 0xffff) % (26+26);
  777. -        if (pid < 26)
  778. -            c = pid + 'A';
  779. -        else
  780. -            c = (pid - 26) + 'a';
  781. -        *trv-- = c;
  782. +            // CHANGE arc4random() -> random()
  783. +            int pid = (qrand() & 0xffff) % (26+26);
  784. +            if (pid < 26)
  785. +                c = pid + 'A';
  786. +            else
  787. +                c = (pid - 26) + 'a';
  788. +            *trv-- = c;
  789. +        }
  790. +        start = trv + 1;
  791.      }
  792. -    start = trv + 1;
  793.  
  794.      for (;;) {
  795. +        // Atomically create file and obtain handle
  796.  #ifndef Q_OS_WIN
  797.          {
  798.              int fd = QT_OPEN(path, QT_OPEN_CREAT | O_EXCL | QT_OPEN_RDWR | QT_OPEN_LARGEFILE, 0600);
  799. @@ -158,6 +164,8 @@ static int _gettemp(char *path, int slen)
  800.  
  801.          /* tricky little algorwwithm for backward compatibility */
  802.          for (trv = start;;) {
  803. +            // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
  804. +            // String progression: "ZZaiC" => "aabiC"
  805.              if (!*trv)
  806.                  return -1;
  807.              if (*trv == 'Z') {
  808. --
  809. 1.7.3.4
  810.  
  811.  
  812. From 8abe099089ec1c9e633fe33e9d79a345018b7aaa Mon Sep 17 00:00:00 2001
  813. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  814. Date: Fri, 25 Mar 2011 16:41:52 +0100
  815. Subject: [PATCH 09/14] Remove duplication with QCoreApplication::applicationId
  816.  
  817. The duplication was there to allow qmake and other tools to compile
  818. without a full build of QtCore. Cleaning up this code justifies losing
  819. the benefit of PID-tagged temporary files in those tools.
  820.  
  821. Reviewed-by: ?
  822. ---
  823. src/corelib/io/qtemporaryfile.cpp |   24 +++++-------------------
  824.  1 files changed, 5 insertions(+), 19 deletions(-)
  825.  
  826. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  827. index 939ea34..fe94964 100644
  828. --- a/src/corelib/io/qtemporaryfile.cpp
  829. +++ b/src/corelib/io/qtemporaryfile.cpp
  830. @@ -61,12 +61,8 @@
  831.  # include "private/qcore_unix_p.h"      // overrides QT_OPEN
  832.  #endif
  833.  
  834. -#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
  835. -# include <process.h>
  836. -#endif
  837. -
  838. -#if defined(Q_OS_VXWORKS)
  839. -#  include <taskLib.h>
  840. +#if defined(QT_BUILD_CORE_LIB)
  841. +#include "qcoreapplication.h"
  842.  #endif
  843.  
  844.  QT_BEGIN_NAMESPACE
  845. @@ -115,23 +111,13 @@ static int _gettemp(char *path, int slen)
  846.  
  847.      // Initialize placeholder with random chars + PID.
  848.      {
  849. -#if defined(Q_OS_WIN)
  850. -        int pid;
  851. -#else
  852. -        pid_t pid;
  853. -#endif
  854. -
  855. -#if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
  856. -        pid = _getpid();
  857. -#elif defined(Q_OS_VXWORKS)
  858. -        pid = (pid_t) taskIdCurrent;
  859. -#else
  860. -        pid = getpid();
  861. -#endif
  862. +#if defined(QT_BUILD_CORE_LIB)
  863. +        qint64 pid = QCoreApplication::applicationPid();
  864.          while (trv >= path && *trv == 'X' && pid != 0) {
  865.              *trv-- = (pid % 10) + '0';
  866.              pid /= 10;
  867.          }
  868. +#endif
  869.  
  870.          while (trv >= path && *trv == 'X') {
  871.              char c;
  872. --
  873. 1.7.3.4
  874.  
  875.  
  876. From 27de64cf56552202f8aa298c32f6b94f83dd56cd Mon Sep 17 00:00:00 2001
  877. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  878. Date: Mon, 28 Mar 2011 18:44:09 +0200
  879. Subject: [PATCH 10/14] Split argument validation of _gettemp as separate function
  880.  
  881. This removes knowledge about the structure of the filename template from
  882. the randomization process and is a pre-requisite to adding Unicode
  883. support to the code.
  884.  
  885. Task-number: QTBUG-4796
  886. Reviewed-by: ?
  887. ---
  888. src/corelib/io/qtemporaryfile.cpp |   76 +++++++++++++++++++++++++------------
  889.  1 files changed, 51 insertions(+), 25 deletions(-)
  890.  
  891. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  892. index fe94964..f6e3426 100644
  893. --- a/src/corelib/io/qtemporaryfile.cpp
  894. +++ b/src/corelib/io/qtemporaryfile.cpp
  895. @@ -95,9 +95,12 @@ QT_BEGIN_NAMESPACE
  896.   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  897.   * SUCH DAMAGE.
  898.   */
  899. +static int createFileFromTemplate(char *const path,
  900. +        char *const placeholderStart, char *const placeholderEnd);
  901. +
  902.  static int _gettemp(char *path, int slen)
  903.  {
  904. -    char *start, *trv, *suffp;
  905. +    char *trv, *suffp;
  906.  
  907.      for (trv = path; *trv; ++trv)
  908.          ;
  909. @@ -109,28 +112,51 @@ static int _gettemp(char *path, int slen)
  910.          return -1;
  911.      }
  912.  
  913. +    while (trv >= path && *trv == 'X')
  914. +        --trv;
  915. +
  916. +    char *const placeholderStart = trv + 1;
  917. +    char *const placeholderEnd = suffp;
  918. +
  919. +    return createFileFromTemplate(path, placeholderStart, placeholderEnd);
  920. +}
  921. +
  922. +/*!
  923. +    \internal
  924. +
  925. +    Generates a unique file path and returns a native handle to the open file.
  926. +    \a path is used as a template when generating unique paths,
  927. +    \a placeholderStart and \a placeholderEnd delimit the sub-string that will
  928. +    be randomized.
  929. +
  930. +    Returns an open handle to the newly created file if successful, an invalid
  931. +    handle otherwise. In both cases, the string in \a path will be changed and
  932. +    contain the generated path name.
  933. +*/
  934. +static int createFileFromTemplate(char *const path,
  935. +        char *const placeholderStart, char *const placeholderEnd)
  936. +{
  937. +    Q_ASSERT(placeholderEnd > placeholderStart);
  938. +
  939.      // Initialize placeholder with random chars + PID.
  940.      {
  941. +        char *rIter = placeholderEnd;
  942. +
  943.  #if defined(QT_BUILD_CORE_LIB)
  944.          qint64 pid = QCoreApplication::applicationPid();
  945. -        while (trv >= path && *trv == 'X' && pid != 0) {
  946. -            *trv-- = (pid % 10) + '0';
  947. +        do {
  948. +            *--rIter = (pid % 10) + '0';
  949.              pid /= 10;
  950. -        }
  951. +        } while (rIter != placeholderStart && pid != 0);
  952.  #endif
  953.  
  954. -        while (trv >= path && *trv == 'X') {
  955. -            char c;
  956. -
  957. -            // CHANGE arc4random() -> random()
  958. -            int pid = (qrand() & 0xffff) % (26+26);
  959. -            if (pid < 26)
  960. -                c = pid + 'A';
  961. +        while (rIter != placeholderStart) {
  962. +            char ch = char((qrand() & 0xffff) % (26 + 26));
  963. +            if (ch < 26)
  964. +                *--rIter = ch + 'A';
  965.              else
  966. -                c = (pid - 26) + 'a';
  967. -            *trv-- = c;
  968. +                *--rIter = ch - 26 + 'a';
  969.          }
  970. -        start = trv + 1;
  971.      }
  972.  
  973.      for (;;) {
  974. @@ -149,24 +175,24 @@ static int _gettemp(char *path, int slen)
  975.  #endif
  976.  
  977.          /* tricky little algorwwithm for backward compatibility */
  978. -        for (trv = start;;) {
  979. +        for (char *iter = placeholderStart;;) {
  980.              // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
  981.              // String progression: "ZZaiC" => "aabiC"
  982. -            if (!*trv)
  983. +            if (!*iter)
  984.                  return -1;
  985. -            if (*trv == 'Z') {
  986. -                if (trv == suffp)
  987. +            if (*iter == 'Z') {
  988. +                if (iter == placeholderEnd)
  989.                      return -1;
  990. -                *trv++ = 'a';
  991. +                *iter++ = 'a';
  992.              } else {
  993. -                if (isdigit(*trv))
  994. -                    *trv = 'a';
  995. -                else if (*trv == 'z') /* inc from z to A */
  996. -                    *trv = 'A';
  997. +                if (isdigit(*iter))
  998. +                    *iter = 'a';
  999. +                else if (*iter == 'z') /* inc from z to A */
  1000. +                    *iter = 'A';
  1001.                  else {
  1002. -                    if (trv == suffp)
  1003. +                    if (iter == placeholderEnd)
  1004.                          return -1;
  1005. -                    ++*trv;
  1006. +                    ++*iter;
  1007.                  }
  1008.                  break;
  1009.              }
  1010. --
  1011. 1.7.3.4
  1012.  
  1013.  
  1014. From ac11acdf579d93517f1b3ded64ee20a13c9a7f73 Mon Sep 17 00:00:00 2001
  1015. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1016. Date: Tue, 29 Mar 2011 17:55:04 +0200
  1017. Subject: [PATCH 11/14] Don't overrun XXXXXX placeholder when generating next filename
  1018.  
  1019. Checking of boundary condition needs to happen whenever there is a
  1020. change. Checking anywhere else is useless.
  1021.  
  1022. This fixes an issue where we would overrun the placeholder if there was
  1023. a 'Z' next to the placeholder string.
  1024.  
  1025. Reviewed-by: ?
  1026. ---
  1027. src/corelib/io/qtemporaryfile.cpp |    4 +---
  1028.  1 files changed, 1 insertions(+), 3 deletions(-)
  1029.  
  1030. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  1031. index f6e3426..4ff0ee3 100644
  1032. --- a/src/corelib/io/qtemporaryfile.cpp
  1033. +++ b/src/corelib/io/qtemporaryfile.cpp
  1034. @@ -181,17 +181,15 @@ static int createFileFromTemplate(char *const path,
  1035.              if (!*iter)
  1036.                  return -1;
  1037.              if (*iter == 'Z') {
  1038. +                *iter++ = 'a';
  1039.                  if (iter == placeholderEnd)
  1040.                      return -1;
  1041. -                *iter++ = 'a';
  1042.              } else {
  1043.                  if (isdigit(*iter))
  1044.                      *iter = 'a';
  1045.                  else if (*iter == 'z') /* inc from z to A */
  1046.                      *iter = 'A';
  1047.                  else {
  1048. -                    if (iter == placeholderEnd)
  1049. -                        return -1;
  1050.                      ++*iter;
  1051.                  }
  1052.                  break;
  1053. --
  1054. 1.7.3.4
  1055.  
  1056.  
  1057. From 791b819fa707fc758a36e82dacc317152b5dc1e9 Mon Sep 17 00:00:00 2001
  1058. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1059. Date: Wed, 30 Mar 2011 16:59:11 +0200
  1060. Subject: [PATCH 12/14] Check for null no longer needed
  1061.  
  1062. With proper checks for boundary conditions, we no longer need to protect
  1063. against reaching the terminating null character.
  1064.  
  1065. Reviewed-by: ?
  1066. ---
  1067. src/corelib/io/qtemporaryfile.cpp |    2 --
  1068.  1 files changed, 0 insertions(+), 2 deletions(-)
  1069.  
  1070. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  1071. index 4ff0ee3..1d9466d 100644
  1072. --- a/src/corelib/io/qtemporaryfile.cpp
  1073. +++ b/src/corelib/io/qtemporaryfile.cpp
  1074. @@ -178,8 +178,6 @@ static int createFileFromTemplate(char *const path,
  1075.          for (char *iter = placeholderStart;;) {
  1076.              // Character progression: [0-9] => 'a' ... 'z' => 'A' .. 'Z'
  1077.              // String progression: "ZZaiC" => "aabiC"
  1078. -            if (!*iter)
  1079. -                return -1;
  1080.              if (*iter == 'Z') {
  1081.                  *iter++ = 'a';
  1082.                  if (iter == placeholderEnd)
  1083. --
  1084. 1.7.3.4
  1085.  
  1086.  
  1087. From 42f2817abf6496b65bde1ce8c09d629286dad853 Mon Sep 17 00:00:00 2001
  1088. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1089. Date: Wed, 30 Mar 2011 13:45:40 +0200
  1090. Subject: [PATCH 13/14] Prefer use of QByteArray to qstrdup and manual memory management
  1091.  
  1092. Reviewed-by: ?
  1093. ---
  1094. src/corelib/io/qtemporaryfile.cpp |   15 ++++++---------
  1095.  1 files changed, 6 insertions(+), 9 deletions(-)
  1096.  
  1097. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  1098. index 1d9466d..92065cb 100644
  1099. --- a/src/corelib/io/qtemporaryfile.cpp
  1100. +++ b/src/corelib/io/qtemporaryfile.cpp
  1101. @@ -278,10 +278,11 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1102.          qfilename += QLatin1String(".XXXXXX");
  1103.  
  1104.      int suffixLength = qfilename.length() - (qfilename.lastIndexOf(QLatin1String("XXXXXX"), -1, Qt::CaseSensitive) + 6);
  1105. -    char *filename = qstrdup(qfilename.toLocal8Bit());
  1106. +    QByteArray filename = qfilename.toLocal8Bit();
  1107. +    char *path = filename.data();
  1108.  
  1109.  #ifndef Q_OS_WIN
  1110. -    int fd = _gettemp(filename, suffixLength);
  1111. +    int fd = _gettemp(path, suffixLength);
  1112.      if (fd != -1) {
  1113.          // First open the fd as an external file descriptor to
  1114.          // initialize the engine properly.
  1115. @@ -291,26 +292,22 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1116.              d->closeFileHandle = true;
  1117.  
  1118.              // Restore the file names (open() resets them).
  1119. -            d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename)); //note that filename is NOT a native path
  1120. +            d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(path, filename.length())); //note that filename is NOT a native path
  1121.              filePathIsTemplate = false;
  1122. -            delete [] filename;
  1123.              return true;
  1124.          }
  1125.  
  1126.          QT_CLOSE(fd);
  1127.      }
  1128. -    delete [] filename;
  1129.      setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  1130.      return false;
  1131.  #else
  1132. -    if (_gettemp(filename, suffixLength) == -1) {
  1133. -        delete [] filename;
  1134. +    if (_gettemp(path, suffixLength) == -1) {
  1135.          return false;
  1136.      }
  1137.  
  1138.      QString template_ = d->fileEntry.filePath();
  1139. -    d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(filename));
  1140. -    delete [] filename;
  1141. +    d->fileEntry = QFileSystemEntry(QString::fromLocal8Bit(path, filename.length()));
  1142.  
  1143.      if (QFSFileEngine::open(openMode)) {
  1144.          filePathIsTemplate = false;
  1145. --
  1146. 1.7.3.4
  1147.  
  1148.  
  1149. From 318277c7aaa28f5d91e28a49e9e1b91ac44ccf8f Mon Sep 17 00:00:00 2001
  1150. From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= <joao.abecasis@nokia.com>
  1151. Date: Wed, 30 Mar 2011 14:27:00 +0200
  1152. Subject: [PATCH 14/14] QTemporaryFile: Locate placeholder before re-encoding
  1153.  
  1154. Previously, the placeholder was identified in unicode-based QString, and
  1155. the offset was assumed to be correct after conversion to the local 8-bit
  1156. encoding. This assumption is obviously wrong for variable-sized
  1157. encoding, such as utf-8.
  1158.  
  1159. With this change, the placeholder is still identified in the original
  1160. QString, but conversion to 8-bit encoding is done in a step-wise fashion
  1161. and offsets recomputed. This allows us to drop the intermediary argument
  1162. verification in _gettemp.
  1163.  
  1164. Task-number: QTBUG-4796
  1165. Reviewed-by: ?
  1166. ---
  1167. src/corelib/io/qtemporaryfile.cpp |   71 +++++++++++++++++++++----------------
  1168.  1 files changed, 40 insertions(+), 31 deletions(-)
  1169.  
  1170. diff --git a/src/corelib/io/qtemporaryfile.cpp b/src/corelib/io/qtemporaryfile.cpp
  1171. index 92065cb..34c01a6 100644
  1172. --- a/src/corelib/io/qtemporaryfile.cpp
  1173. +++ b/src/corelib/io/qtemporaryfile.cpp
  1174. @@ -95,31 +95,6 @@ QT_BEGIN_NAMESPACE
  1175.   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1176.   * SUCH DAMAGE.
  1177.   */
  1178. -static int createFileFromTemplate(char *const path,
  1179. -        char *const placeholderStart, char *const placeholderEnd);
  1180. -
  1181. -static int _gettemp(char *path, int slen)
  1182. -{
  1183. -    char *trv, *suffp;
  1184. -
  1185. -    for (trv = path; *trv; ++trv)
  1186. -        ;
  1187. -    trv -= slen;
  1188. -    suffp = trv;
  1189. -    --trv;
  1190. -    if (trv < path) {
  1191. -        errno = EINVAL;
  1192. -        return -1;
  1193. -    }
  1194. -
  1195. -    while (trv >= path && *trv == 'X')
  1196. -        --trv;
  1197. -
  1198. -    char *const placeholderStart = trv + 1;
  1199. -    char *const placeholderEnd = suffp;
  1200. -
  1201. -    return createFileFromTemplate(path, placeholderStart, placeholderEnd);
  1202. -}
  1203.  
  1204.  /*!
  1205.      \internal
  1206. @@ -274,15 +249,49 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1207.          return QFSFileEngine::open(openMode);
  1208.  
  1209.      QString qfilename = d->fileEntry.filePath();
  1210. -    if(!qfilename.contains(QLatin1String("XXXXXX")))
  1211. -        qfilename += QLatin1String(".XXXXXX");
  1212.  
  1213. -    int suffixLength = qfilename.length() - (qfilename.lastIndexOf(QLatin1String("XXXXXX"), -1, Qt::CaseSensitive) + 6);
  1214. -    QByteArray filename = qfilename.toLocal8Bit();
  1215. +    // Find placeholder string.
  1216. +    size_t phPos = size_t(qfilename.length());
  1217. +    size_t phLength = 0;
  1218. +
  1219. +    while (phPos != 0) {
  1220. +        --phPos;
  1221. +
  1222. +        if (qfilename[phPos] == QLatin1Char('X')) {
  1223. +            ++phLength;
  1224. +            continue;
  1225. +        }
  1226. +
  1227. +        if (qfilename[phPos] == QLatin1Char('/')
  1228. +                || phLength >= 6) {
  1229. +            ++phPos;
  1230. +            break;
  1231. +        }
  1232. +
  1233. +        phLength = 0;
  1234. +    }
  1235. +
  1236. +    QStringRef prefix, suffix;
  1237. +    if (phLength < 6) {
  1238. +        qfilename += QLatin1Char('.');
  1239. +        prefix = QStringRef(&qfilename);
  1240. +        phLength = 6;
  1241. +    } else {
  1242. +        prefix = qfilename.leftRef(phPos);
  1243. +        suffix = qfilename.midRef(phPos + phLength);
  1244. +    }
  1245. +
  1246. +    QByteArray filename = prefix.toLocal8Bit();
  1247. +    phPos = filename.length();
  1248. +    if (suffix.isEmpty())
  1249. +        filename.resize(phPos + phLength);
  1250. +    else
  1251. +        filename.insert(phPos + phLength, suffix.toLocal8Bit());
  1252. +
  1253.      char *path = filename.data();
  1254.  
  1255.  #ifndef Q_OS_WIN
  1256. -    int fd = _gettemp(path, suffixLength);
  1257. +    int fd = createFileFromTemplate(path, path + phPos, path + phPos + phLength);
  1258.      if (fd != -1) {
  1259.          // First open the fd as an external file descriptor to
  1260.          // initialize the engine properly.
  1261. @@ -302,7 +311,7 @@ bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode)
  1262.      setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, qt_error_string(errno));
  1263.      return false;
  1264.  #else
  1265. -    if (_gettemp(path, suffixLength) == -1) {
  1266. +    if (createFileFromTemplate(path, path + phPos, path + phPos + phLength) == -1) {
  1267.          return false;
  1268.      }
  1269.  
  1270. --
  1271. 1.7.3.4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement