Make QFile::open fail when using an invalid file name

Fixes the bug in QFile which allowed opening a file with reserved
characters in its name.  If the name is a long file path, CreateFile
opens a file with a truncated name instead of failing, so we have
to catch reserved characters ourselves.

[ChangeLog][Windows] Fixed a bug that caused QFile to create
files with truncated names if the file name was invalid. Now,
QFile::open correctly fails to create such files.

Task-number: QTBUG-57023
Change-Id: I01d5a7132054cecdfa839d0b8de06460039248a3
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Jesus Fernandez 2016-11-10 00:52:57 +01:00 committed by Jesus Fernandez
parent 99adabdb7e
commit 346cd79192
2 changed files with 15 additions and 0 deletions

View File

@ -108,6 +108,20 @@ bool QFSFileEnginePrivate::nativeOpen(QIODevice::OpenMode openMode)
{
Q_Q(QFSFileEngine);
// Check if the file name is valid:
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#naming_conventions
const QString fileName = fileEntry.fileName();
for (QString::const_iterator it = fileName.constBegin(), end = fileName.constEnd();
it != end; ++it) {
const QChar c = *it;
if (c == QLatin1Char('<') || c == QLatin1Char('>') || c == QLatin1Char(':') ||
c == QLatin1Char('\"') || c == QLatin1Char('/') || c == QLatin1Char('\\') ||
c == QLatin1Char('|') || c == QLatin1Char('?') || c == QLatin1Char('*')) {
q->setError(QFile::OpenError, QStringLiteral("Invalid file name"));
return false;
}
}
// All files are opened in share mode (both read and write).
DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;

View File

@ -1141,6 +1141,7 @@ void tst_QFile::invalidFile_data()
#else
#if !defined(Q_OS_WINRT)
QTest::newRow( "colon2" ) << invalidDriveLetter() + QString::fromLatin1(":ail:invalid");
QTest::newRow( "date" ) << QString( "testLog-03:20.803Z.txt" );
#endif
QTest::newRow( "colon3" ) << QString( ":failinvalid" );
QTest::newRow( "forwardslash" ) << QString( "fail/invalid" );