diff --git a/kdecore/io/ktemporaryfile.cpp b/kdecore/io/ktemporaryfile.cpp index b73fdb94..06d366c0 100644 --- a/kdecore/io/ktemporaryfile.cpp +++ b/kdecore/io/ktemporaryfile.cpp @@ -79,7 +79,6 @@ void KTemporaryFile::setSuffix(const QString &suffix) QString KTemporaryFile::filePath(const QString &pathtemplate) { - static QChar xchar = QChar::fromLatin1('X'); static QChar underscorechar = QChar::fromLatin1('_'); static const char tmpnamechars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -94,10 +93,14 @@ QString KTemporaryFile::filePath(const QString &pathtemplate) } QString result = pathtemplate; - int xindex = result.indexOf(xchar); - while (xindex != -1) { - result.replace(xindex, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)])); - xindex = result.indexOf(xchar, xindex + 1); + int counter = result.size(); + while (counter) { + counter--; + if (result.at(counter) == QLatin1Char('/')) { + break; + } else if (result.at(counter) == QLatin1Char('X')) { + result.replace(counter, 1, QChar::fromLatin1(tmpnamechars[KRandom::randomMax(52)])); + } } if (!QDir::isAbsolutePath(result)) { result.prepend(underscorechar); diff --git a/kdecore/tests/ktemporaryfiletest.cpp b/kdecore/tests/ktemporaryfiletest.cpp index e8d8a597..090e8eb1 100644 --- a/kdecore/tests/ktemporaryfiletest.cpp +++ b/kdecore/tests/ktemporaryfiletest.cpp @@ -109,4 +109,8 @@ void KTemporaryFileTest::testFilePath() tmpfilepath = KTemporaryFile::filePath("/foo/bar/XXXXX.tmp"); QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/bar/"))); QVERIFY(!tmpfilepath.contains("XXXXX")); + + tmpfilepath = KTemporaryFile::filePath("/foo/X/bar/XXXXX.tmp"); + QVERIFY(tmpfilepath.startsWith(QLatin1String("/foo/X/bar/"))); + QVERIFY(!tmpfilepath.contains("XXXXX")); }