From 705d7730dc050429f821533770492b8ceae7d64e Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 21 Jun 2016 14:28:01 +0800 Subject: [PATCH 1/3] Fix upper-bound limit of command line for "Run External Program" in Windows. Closes #5399. --- src/app/application.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 622f6b307..aab9a9aed 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -262,8 +262,9 @@ void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) c QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); #elif defined(Q_OS_WIN) // test cmd: `echo "%F" > "c:\ab ba.txt"` program.prepend(QLatin1String("cmd.exe /C ")); - if (program.size() >= MAX_PATH) { - logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(MAX_PATH), Log::CRITICAL); + const uint cmdMaxLength = 32768; // max length (incl. terminate char) for `lpCommandLine` in `CreateProcessW()` + if ((program.size() + 1) > cmdMaxLength) { + logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(cmdMaxLength), Log::CRITICAL); return; } From 3846a5b8759fc45dce2cd2c12c711ac2588c923f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 21 Jun 2016 15:23:22 +0800 Subject: [PATCH 2/3] Invoke system's cmd.exe directly. --- src/app/application.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index aab9a9aed..dd571eab6 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -261,7 +261,12 @@ void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) c #if defined(Q_OS_UNIX) QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); #elif defined(Q_OS_WIN) // test cmd: `echo "%F" > "c:\ab ba.txt"` - program.prepend(QLatin1String("cmd.exe /C ")); + static const QString cmdPath = []() -> QString { + WCHAR systemPath[64] = {0}; + GetSystemDirectoryW(systemPath, sizeof(systemPath) / sizeof(WCHAR)); + return QString::fromWCharArray(systemPath) + QLatin1String("\\cmd.exe /C "); + }(); + program.prepend(cmdPath); const uint cmdMaxLength = 32768; // max length (incl. terminate char) for `lpCommandLine` in `CreateProcessW()` if ((program.size() + 1) > cmdMaxLength) { logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(cmdMaxLength), Log::CRITICAL); From 1e1471c7c612dd494fe71cc2f849737944b173b7 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 16 Jul 2016 10:07:48 +0800 Subject: [PATCH 3/3] Workaround space issues in file path References: https://github.com/qbittorrent/qBittorrent/issues/5439#issuecomment-228616817 https://github.com/qbittorrent/qBittorrent/issues/5439#issuecomment-232214712 --- src/app/application.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/application.cpp b/src/app/application.cpp index dd571eab6..2e1a378e2 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -266,6 +266,7 @@ void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) c GetSystemDirectoryW(systemPath, sizeof(systemPath) / sizeof(WCHAR)); return QString::fromWCharArray(systemPath) + QLatin1String("\\cmd.exe /C "); }(); + program.prepend(QLatin1String("\"")).append(QLatin1String("\"")); program.prepend(cmdPath); const uint cmdMaxLength = 32768; // max length (incl. terminate char) for `lpCommandLine` in `CreateProcessW()` if ((program.size() + 1) > cmdMaxLength) {