kdecore: search backwards for the template and stop at directory separator in KTemporaryFile::filePath()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-11-01 00:51:55 +02:00
parent 49371e1370
commit 95a8d172be
2 changed files with 12 additions and 5 deletions

View file

@ -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);

View file

@ -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"));
}