New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MumbleApplication: introduce getenvQString and use it in applicationVersionRoot. #2808
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,54 @@ | |
#include "GlobalShortcut.h" | ||
#include "Global.h" | ||
|
||
// getenvQString is a wrapper around _wgetenv_s (on Windows) | ||
// and getenv (on everything else). | ||
// | ||
// On Windows, it expects a Unicode environment -- so variables | ||
// are expected to be UTF16. | ||
// On everthing else, it expects the environment variables to be | ||
// UTF-8 encoded. | ||
static QString getenvQString(QString name) { | ||
#ifdef Q_OS_WIN | ||
QByteArray buf; | ||
size_t requiredSize = 0; | ||
|
||
static_assert(sizeof(wchar_t) == sizeof(ushort), "expected 2-byte wchar_t"); | ||
|
||
const wchar_t *wname = reinterpret_cast<const wchar_t *>(name.utf16()); | ||
|
||
// Query the required buffer size (in elements). | ||
_wgetenv_s(&requiredSize, 0, 0, wname); | ||
if (requiredSize == 0) { | ||
return QString(); | ||
} | ||
|
||
// Resize buf to fit the value and put it there. | ||
buf.resize(static_cast<int>(requiredSize * sizeof(wchar_t))); | ||
_wgetenv_s(&requiredSize, reinterpret_cast<wchar_t *>(buf.data()), requiredSize * sizeof(wchar_t), wname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The size paramter is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally missed that. Yeah. That should be fixed (even though it can't really break anything outside of a race condition). |
||
|
||
// Find the length of the buffer without | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't have Not that the code is wrong. So no use blocking the merge. |
||
// counting NUL elements. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would we get NUL elements from after we made buf the size it needs to be for all its content? |
||
const wchar_t *wbuf = reinterpret_cast<const wchar_t *>(buf.constData()); | ||
size_t len = 0; | ||
for (len = 0; len < requiredSize; len++) { | ||
if (wbuf[len] == 0) { | ||
break; | ||
} | ||
} | ||
|
||
// Convert the value to QString and return it. | ||
return QString::fromWCharArray(wbuf, static_cast<int>(len)); | ||
#else | ||
QByteArray nameU8 = name.toUtf8(); | ||
char *val = ::getenv(nameU8.constData()); | ||
if (val == NULL) { | ||
return QString(); | ||
} | ||
return QString::fromUtf8(val); | ||
#endif | ||
} | ||
|
||
MumbleApplication *MumbleApplication::instance() { | ||
return static_cast<MumbleApplication *>(QCoreApplication::instance()); | ||
} | ||
|
@@ -25,9 +73,9 @@ MumbleApplication::MumbleApplication(int &pargc, char **pargv) | |
} | ||
|
||
QString MumbleApplication::applicationVersionRootPath() { | ||
QByteArray versionRoot = qgetenv("MUMBLE_VERSION_ROOT"); | ||
QString versionRoot = getenvQString(QLatin1String("MUMBLE_VERSION_ROOT")); | ||
if (versionRoot.count() > 0) { | ||
return QString::fromUtf8(versionRoot.constData()); | ||
return versionRoot; | ||
} | ||
return this->applicationDirPath(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So whose bug is this? This function not working like before surely breaks lots of other stuff out there.