Win: qdiriterator bench: fix missing null-terminator issues

It's not done by toWCharArray. This caused some issues as we were using
API requiring a null-terminator. wcslen for instance was measuring the
string as being millions of characters long, causing fairly quick
crashes when appending.

Change-Id: Iedaaf9f195be22a610543ab649da92a87cb71973
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Mårten Nordheim 2019-05-21 14:48:57 +02:00
parent 94aa350621
commit 16158e1132
2 changed files with 15 additions and 14 deletions

View File

@ -78,24 +78,22 @@ void tst_qdiriterator::data()
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
static int posix_helper(const wchar_t *dirpath) static int posix_helper(const wchar_t *dirpath, size_t length)
{ {
int count = 0; int count = 0;
HANDLE hSearch; HANDLE hSearch;
WIN32_FIND_DATA fd; WIN32_FIND_DATA fd;
const size_t origDirPathLength = wcslen(dirpath);
wchar_t appendedPath[MAX_PATH]; wchar_t appendedPath[MAX_PATH];
wcscpy(appendedPath, dirpath); Q_ASSERT(MAX_PATH > length + 3);
wcscat(appendedPath, L"\\*"); wcsncpy(appendedPath, dirpath, length);
wcscpy(appendedPath + length, L"\\*");
#ifndef Q_OS_WINRT #ifndef Q_OS_WINRT
hSearch = FindFirstFile(appendedPath, &fd); hSearch = FindFirstFile(appendedPath, &fd);
#else #else
hSearch = FindFirstFileEx(appendedPath, FindExInfoStandard, &fd, hSearch = FindFirstFileEx(appendedPath, FindExInfoStandard, &fd,
FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH); FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH);
#endif #endif
appendedPath[origDirPathLength] = 0;
if (hSearch == INVALID_HANDLE_VALUE) { if (hSearch == INVALID_HANDLE_VALUE) {
qWarning("FindFirstFile failed"); qWarning("FindFirstFile failed");
@ -107,10 +105,12 @@ static int posix_helper(const wchar_t *dirpath)
!(fd.cFileName[0] == L'.' && fd.cFileName[1] == L'.' && fd.cFileName[2] == 0)) !(fd.cFileName[0] == L'.' && fd.cFileName[1] == L'.' && fd.cFileName[2] == 0))
{ {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
wcscat(appendedPath, L"\\"); // newLength will "point" to where we put a * earlier, so we overwrite that.
wcscat(appendedPath, fd.cFileName); size_t newLength = length + 1; // "+ 1" for directory separator
count += posix_helper(appendedPath); Q_ASSERT(newLength + wcslen(fd.cFileName) + 1 < MAX_PATH); // "+ 1" for null-terminator
appendedPath[origDirPathLength] = 0; wcscpy(appendedPath + newLength, fd.cFileName);
newLength += wcslen(fd.cFileName);
count += posix_helper(appendedPath, newLength);
} }
else { else {
++count; ++count;
@ -164,8 +164,8 @@ void tst_qdiriterator::posix()
QBENCHMARK { QBENCHMARK {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
wchar_t wPath[MAX_PATH]; wchar_t wPath[MAX_PATH];
path.toWCharArray(wPath); const int end = path.toWCharArray(wPath);
count = posix_helper(wPath); count = posix_helper(wPath, end);
#else #else
count = posix_helper(dirpath.constData()); count = posix_helper(dirpath.constData());
#endif #endif

View File

@ -218,8 +218,9 @@ void QFileSystemIteratorPrivate::pushSubDirectory(const QByteArray &path)
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
wchar_t szSearchPath[MAX_PATH]; wchar_t szSearchPath[MAX_PATH];
QString::fromLatin1(path).toWCharArray(szSearchPath); const int end = QString::fromLatin1(path + "\\*").toWCharArray(szSearchPath);
wcscat(szSearchPath, L"\\*"); Q_ASSERT(end < MAX_PATH);
szSearchPath[end] = L'\0';
#ifndef Q_OS_WINRT #ifndef Q_OS_WINRT
HANDLE dir = FindFirstFile(szSearchPath, &m_fileSearchResult); HANDLE dir = FindFirstFile(szSearchPath, &m_fileSearchResult);
#else #else