Bug 1460996: Ensure the bootstrap process is always working with an argv[0] containing an absolute path (v2); r=mhowell

This commit is contained in:
Aaron Klotz 2018-05-14 11:40:08 -06:00
Родитель 017a4c00fe
Коммит bf395a240e
1 изменённых файлов: 26 добавлений и 9 удалений

Просмотреть файл

@ -80,7 +80,31 @@ ShowError(DWORD aError = ::GetLastError())
::LocalFree(rawMsgBuf);
}
static wchar_t gAbsPath[MAX_PATH];
static bool
SetArgv0ToFullBinaryPath(wchar_t* aArgv[])
{
DWORD bufLen = MAX_PATH;
mozilla::UniquePtr<wchar_t[]> buf;
while (true) {
buf = mozilla::MakeUnique<wchar_t[]>(bufLen);
DWORD retLen = ::GetModuleFileNameW(nullptr, buf.get(), bufLen);
if (!retLen) {
return false;
}
if (retLen == bufLen && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
bufLen *= 2;
continue;
}
break;
}
// We intentionally leak buf into argv[0]
aArgv[0] = buf.release();
return true;
}
namespace mozilla {
@ -112,18 +136,11 @@ LauncherMain(int argc, wchar_t* argv[])
}
}
// Convert argv[0] to an absolute path if necessary
DWORD absPathLen = ::SearchPathW(nullptr, argv[0], L".exe",
ArrayLength(gAbsPath), gAbsPath, nullptr);
if (!absPathLen) {
if (!SetArgv0ToFullBinaryPath(argv)) {
ShowError();
return 1;
}
if (absPathLen < ArrayLength(gAbsPath)) {
argv[0] = gAbsPath;
}
// If we're elevated, we should relaunch ourselves as a normal user
Maybe<bool> isElevated = IsElevated();
if (!isElevated) {