diff options
author | Ivan Komissarov <ABBAPOH@gmail.com> | 2020-02-02 07:25:51 +0100 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2020-02-03 13:54:03 +0000 |
commit | c5fc7db1cd5c3149aae8e7bc40e89e6d188f9dc8 (patch) | |
tree | ac2797203a7b1a320794ab9a6b8b878c9eba259a /src | |
parent | 6a945fd22293fb81e0ec6ff75cac9dc7346c230d (diff) |
Fix parseCommandLine() function
'buf' variable was leaking in case of an error. 'args' variable was
leaking in the success case (it has to be freed with LocalFree). Also,
reserve the necessary amount of elements in the result string list
Change-Id: Ia2a1ea7350baa07533e3f742926f2a9cc12bb29c
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/corelib/tools/msvcinfo.cpp | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/lib/corelib/tools/msvcinfo.cpp b/src/lib/corelib/tools/msvcinfo.cpp index 4e71950e8..73d22d329 100644 --- a/src/lib/corelib/tools/msvcinfo.cpp +++ b/src/lib/corelib/tools/msvcinfo.cpp @@ -54,6 +54,7 @@ #endif #include <algorithm> +#include <memory> #include <mutex> using namespace qbs; @@ -129,16 +130,18 @@ public: #ifdef Q_OS_WIN static QStringList parseCommandLine(const QString &commandLine) { - QStringList list; - const auto buf = new wchar_t[commandLine.size() + 1]; - buf[commandLine.toWCharArray(buf)] = 0; + const auto buf = std::make_unique<wchar_t[]>(size_t(commandLine.size()) + 1); + buf[size_t(commandLine.toWCharArray(buf.get()))] = 0; int argCount = 0; - LPWSTR *args = CommandLineToArgvW(buf, &argCount); + const auto argsDeleter = [](LPWSTR *p){ LocalFree(p); }; + const auto args = std::unique_ptr<LPWSTR[], decltype(argsDeleter)>( + CommandLineToArgvW(buf.get(), &argCount), argsDeleter); if (!args) throw ErrorInfo(mkStr("Could not parse command line arguments: ") + commandLine); + QStringList list; + list.reserve(argCount); for (int i = 0; i < argCount; ++i) - list.push_back(QString::fromWCharArray(args[i])); - delete[] buf; + list.push_back(QString::fromWCharArray(args[size_t(i)])); return list; } #endif |