diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-10-24 04:27:22 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-11-06 07:44:53 -0500 |
| commit | 4e8dcc5157706e1478396179f5dc11305532b159 (patch) | |
| tree | c1594fb21b9f377562cd6be020f55626e361216f /src/env.cpp | |
| parent | b60f2aa786cf748e1839f4320604030863279032 (diff) | |
moved findJavaInstallation() and getFileExecutionContext() to spawn
fixed env::get() returning garbage after value
Diffstat (limited to 'src/env.cpp')
| -rw-r--r-- | src/env.cpp | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/src/env.cpp b/src/env.cpp index 78b5dc96..507607d1 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -190,19 +190,36 @@ QString setPath(const QString& s) QString get(const QString& name) { - std::wstring s(4000, L' '); + std::size_t bufferSize = 4000; + auto buffer = std::make_unique<wchar_t[]>(bufferSize); DWORD realSize = ::GetEnvironmentVariableW( - name.toStdWString().c_str(), s.data(), static_cast<DWORD>(s.size())); + name.toStdWString().c_str(), + buffer.get(), static_cast<DWORD>(bufferSize)); - if (realSize > s.size()) { - s.resize(realSize); + if (realSize > bufferSize) { + bufferSize = realSize; + buffer = std::make_unique<wchar_t[]>(bufferSize); - ::GetEnvironmentVariableW( - name.toStdWString().c_str(), s.data(), static_cast<DWORD>(s.size())); + realSize = ::GetEnvironmentVariableW( + name.toStdWString().c_str(), + buffer.get(), static_cast<DWORD>(bufferSize)); } - return QString::fromStdWString(s); + if (realSize == 0) { + const auto e = ::GetLastError(); + + // don't log if not found + if (e != ERROR_ENVVAR_NOT_FOUND) { + log::error( + "failed to get environment variable '{}', {}", + name, formatSystemMessage(e)); + } + + return {}; + } + + return QString::fromWCharArray(buffer.get(), realSize); } QString set(const QString& n, const QString& v) |
