From c621cae43be58b71395170a43ed39adee3d0e646 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 27 Mar 2022 14:17:42 +0800 Subject: [PATCH] Don't use explicit memory management And avoid dangling pointers. Original PR #16705. --- src/app/application.cpp | 8 +++---- src/app/qtlocalpeer/qtlockedfile_win.cpp | 6 ++++-- src/app/stacktrace_win.h | 3 ++- src/base/bittorrent/session.cpp | 3 ++- src/base/utils/foreignapps.cpp | 27 ++++++------------------ src/base/utils/misc.cpp | 4 +++- src/base/utils/misc.h | 8 ++----- src/gui/torrentcontentmodel.cpp | 4 ++-- src/gui/utils.cpp | 3 ++- 9 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 6e104fd32..405114555 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -402,14 +402,13 @@ void Application::runExternalProgram(const BitTorrent::Torrent *torrent) const LogMsg(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program)); #if defined(Q_OS_WIN) - auto programWchar = std::make_unique(program.length() + 1); - program.toWCharArray(programWchar.get()); + const std::wstring programWStr = program.toStdWString(); // Need to split arguments manually because QProcess::startDetached(QString) // will strip off empty parameters. // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"` int argCount = 0; - std::unique_ptr args {::CommandLineToArgvW(programWchar.get(), &argCount), ::LocalFree}; + std::unique_ptr args {::CommandLineToArgvW(programWStr.c_str(), &argCount), ::LocalFree}; QStringList argList; for (int i = 1; i < argCount; ++i) @@ -835,8 +834,9 @@ void Application::cleanup() m_window->hide(); #ifdef Q_OS_WIN + const std::wstring msg = tr("Saving torrent progress...").toStdWString(); ::ShutdownBlockReasonCreate(reinterpret_cast(m_window->effectiveWinId()) - , tr("Saving torrent progress...").toStdWString().c_str()); + , msg.c_str()); #endif // Q_OS_WIN // Do manual cleanup in MainWindow to force widgets diff --git a/src/app/qtlocalpeer/qtlockedfile_win.cpp b/src/app/qtlocalpeer/qtlockedfile_win.cpp index 65bbdf875..cf94dac94 100644 --- a/src/app/qtlocalpeer/qtlockedfile_win.cpp +++ b/src/app/qtlocalpeer/qtlockedfile_win.cpp @@ -87,9 +87,11 @@ Qt::HANDLE QtLockedFile::getMutexHandle(const int idx, const bool doCreate) if (idx >= 0) mname += QString::number(idx); + const std::wstring mnameWStr = mname.toStdWString(); + if (doCreate) { - const Qt::HANDLE mutex = ::CreateMutexW(NULL, FALSE, reinterpret_cast(mname.utf16())); + const Qt::HANDLE mutex = ::CreateMutexW(NULL, FALSE, mnameWStr.c_str()); if (!mutex) { qErrnoWarning("QtLockedFile::lock(): CreateMutex failed"); @@ -100,7 +102,7 @@ Qt::HANDLE QtLockedFile::getMutexHandle(const int idx, const bool doCreate) } else { - const Qt::HANDLE mutex = ::OpenMutexW((SYNCHRONIZE | MUTEX_MODIFY_STATE), FALSE, reinterpret_cast(mname.utf16())); + const Qt::HANDLE mutex = ::OpenMutexW((SYNCHRONIZE | MUTEX_MODIFY_STATE), FALSE, mnameWStr.c_str()); if (!mutex) { if (GetLastError() != ERROR_FILE_NOT_FOUND) diff --git a/src/app/stacktrace_win.h b/src/app/stacktrace_win.h index 7a92d54b1..a5e6e326a 100644 --- a/src/app/stacktrace_win.h +++ b/src/app/stacktrace_win.h @@ -255,9 +255,10 @@ const QString straceWin::getBacktrace() QTextStream logStream(&log); logStream << "```\n"; + const std::wstring appPath = QCoreApplication::applicationDirPath().toStdWString(); HANDLE hProcess = GetCurrentProcess(); HANDLE hThread = GetCurrentThread(); - SymInitializeW(hProcess, QCoreApplication::applicationDirPath().toStdWString().c_str(), TRUE); + SymInitializeW(hProcess, appPath.c_str(), TRUE); DWORD64 dwDisplacement; diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index d3cfcab3f..6c7488256 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -279,8 +279,9 @@ namespace if (!uuid.isNull()) return uuid.toString().toUpper(); // Libtorrent expects the GUID in uppercase + const std::wstring nameWStr = name.toStdWString(); NET_LUID luid {}; - const LONG res = ::ConvertInterfaceNameToLuidW(name.toStdWString().c_str(), &luid); + const LONG res = ::ConvertInterfaceNameToLuidW(nameWStr.c_str(), &luid); if (res == 0) { GUID guid; diff --git a/src/base/utils/foreignapps.cpp b/src/base/utils/foreignapps.cpp index 9020bac2a..6148bc9c8 100644 --- a/src/base/utils/foreignapps.cpp +++ b/src/base/utils/foreignapps.cpp @@ -127,26 +127,17 @@ namespace QString getRegValue(const HKEY handle, const QString &name = {}) { - QString result; - + const std::wstring nameWStr = name.toStdWString(); DWORD type = 0; DWORD cbData = 0; - LPWSTR lpValueName = NULL; - if (!name.isEmpty()) - { - lpValueName = new WCHAR[name.size() + 1]; - name.toWCharArray(lpValueName); - lpValueName[name.size()] = 0; - } // Discover the size of the value - ::RegQueryValueExW(handle, lpValueName, NULL, &type, NULL, &cbData); + ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, NULL, &cbData); DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1; LPWSTR lpData = new WCHAR[cBuffer]; - LONG res = ::RegQueryValueExW(handle, lpValueName, NULL, &type, (LPBYTE)lpData, &cbData); - if (lpValueName) - delete[] lpValueName; + LONG res = ::RegQueryValueExW(handle, nameWStr.c_str(), NULL, &type, (LPBYTE)lpData, &cbData); + QString result; if (res == ERROR_SUCCESS) { lpData[cBuffer - 1] = 0; @@ -185,18 +176,14 @@ namespace bool found = false; while (!found && !versions.empty()) { - const QString version = versions.takeLast() + "\\InstallPath"; - LPWSTR lpSubkey = new WCHAR[version.size() + 1]; - version.toWCharArray(lpSubkey); - lpSubkey[version.size()] = 0; + const std::wstring version = QString(versions.takeLast() + "\\InstallPath").toStdWString(); HKEY hkInstallPath; - res = ::RegOpenKeyExW(hkPythonCore, lpSubkey, 0, samDesired, &hkInstallPath); - delete[] lpSubkey; + res = ::RegOpenKeyExW(hkPythonCore, version.c_str(), 0, samDesired, &hkInstallPath); if (res == ERROR_SUCCESS) { - qDebug("Detected possible Python v%s location", qUtf8Printable(version)); + qDebug("Detected possible Python v%ls location", version.c_str()); path = getRegValue(hkInstallPath); ::RegCloseKey(hkInstallPath); diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index d72db3cd1..1adb227bb 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -31,6 +31,8 @@ #include #ifdef Q_OS_WIN +#include + #include #include #include @@ -393,7 +395,7 @@ QString Utils::Misc::getUserIDString() const int UNLEN = 256; WCHAR buffer[UNLEN + 1] = {0}; DWORD buffer_len = sizeof(buffer) / sizeof(*buffer); - if (GetUserNameW(buffer, &buffer_len)) + if (::GetUserNameW(buffer, &buffer_len)) uid = QString::fromWCharArray(buffer); #else uid = QString::number(getuid()); diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index be0583cdc..a1eb9e1f9 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -31,7 +31,6 @@ #include #ifdef Q_OS_WIN -#include #include #endif @@ -93,14 +92,11 @@ namespace Utils::Misc QString path = windowsSystemPath(); if (!path.endsWith('\\')) path += '\\'; - path += source; - auto pathWchar = std::make_unique(path.length() + 1); - path.toWCharArray(pathWchar.get()); - + const std::wstring pathWStr = path.toStdWString(); return reinterpret_cast( - ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName)); + ::GetProcAddress(::LoadLibraryW(pathWStr.c_str()), funcName)); } #endif // Q_OS_WIN } diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 1eb0176af..610a8fd86 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -113,9 +113,9 @@ namespace { QPixmap pixmapForExtension(const QString &ext) const override { - const QString extWithDot = QLatin1Char('.') + ext; + const std::wstring extWStr = QString(QLatin1Char('.') + ext).toStdWString(); SHFILEINFO sfi {}; - HRESULT hr = ::SHGetFileInfoW(extWithDot.toStdWString().c_str(), + HRESULT hr = ::SHGetFileInfoW(extWStr.c_str(), FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES); if (FAILED(hr)) return {}; diff --git a/src/gui/utils.cpp b/src/gui/utils.cpp index 9894d1cc4..8034351d7 100644 --- a/src/gui/utils.cpp +++ b/src/gui/utils.cpp @@ -172,7 +172,8 @@ void Utils::Gui::openFolderSelect(const QString &absolutePath) { if (SUCCEEDED(::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))) { - PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(reinterpret_cast(Utils::Fs::toNativePath(path).utf16())); + const std::wstring pathWStr = Utils::Fs::toNativePath(path).toStdWString(); + PIDLIST_ABSOLUTE pidl = ::ILCreateFromPathW(pathWStr.c_str()); if (pidl) { ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0);