Bug 1865172 Part 1 - Always store a page name value when a breakpoint is first found...
[gecko.git] / toolkit / xre / nsWindowsWMain.cpp
blob027c8883cc24b4d9ecba8f06e72a2ee99574a0c8
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsWindowsWMain_cpp
6 #define nsWindowsWMain_cpp
8 // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
9 // similar bootstrap code. It converts wide-character windows wmain into UTF-8
10 // narrow-character strings.
12 #ifndef XP_WIN
13 # error This file only makes sense on Windows.
14 #endif
16 #include "mozilla/Char16.h"
17 #include "nsUTF8Utils.h"
19 #include <windows.h>
21 #ifdef __MINGW32__
23 /* MingW currently does not implement a wide version of the
24 startup routines. Workaround is to implement something like
25 it ourselves. See bug 411826 */
27 # include <shellapi.h>
29 int wmain(int argc, WCHAR** argv);
31 int main(int argc, char** argv) {
32 LPWSTR commandLine = GetCommandLineW();
33 int argcw = 0;
34 LPWSTR* argvw = CommandLineToArgvW(commandLine, &argcw);
35 if (!argvw) return 127;
37 int result = wmain(argcw, argvw);
38 LocalFree(argvw);
39 return result;
41 #endif /* __MINGW32__ */
43 #define main NS_internal_main
45 #ifndef XRE_WANT_ENVIRON
46 int main(int argc, char** argv);
47 #else
48 int main(int argc, char** argv, char** envp);
49 #endif
51 // SanitizeEnvironmentVariables()
53 // Mitigate CVE-2007-6753 (binary planting via unexpanded environment variable
54 // references) by forcibly expanding all environment variable references in
55 // %PATH%.
57 // CVE-2007-6753 is documented to have affected all active mainline Windows
58 // versions at the time of its announcement (i.e., up to Windows 7). Microsoft
59 // has never formally stated that later versions of Windows are free of this
60 // issue; out of an abundance of caution, we continue to mitigate it on Windows
61 // 8 and beyond.
62 static void SanitizeEnvironmentVariables() {
63 DWORD bufferSize = GetEnvironmentVariableW(L"PATH", nullptr, 0);
64 if (bufferSize) {
65 wchar_t* originalPath = new wchar_t[bufferSize];
66 if (bufferSize - 1 ==
67 GetEnvironmentVariableW(L"PATH", originalPath, bufferSize)) {
68 bufferSize = ExpandEnvironmentStringsW(originalPath, nullptr, 0);
69 // The maximum length of a Windows environment variable and the maximum
70 // length of a string returned by ExpandEnvironmentStringsW are both
71 // documented to be 32,767 characters. Under some versions of Windows,
72 // however, both may be longer, with delayed but deleterious consequences.
74 // We therefore cap the size manually, in case the user has a sufficiently
75 // problematic %PATH%. (See bug 1753910.) This is unlikely to occur unless
76 // there is some form of recursive referencing involved, in which case
77 // expansion is no mitigation anyway.
78 if (bufferSize && bufferSize < 32768) {
79 wchar_t* newPath = new wchar_t[bufferSize];
80 if (ExpandEnvironmentStringsW(originalPath, newPath, bufferSize)) {
81 SetEnvironmentVariableW(L"PATH", newPath);
83 delete[] newPath;
86 delete[] originalPath;
90 static char* AllocConvertUTF16toUTF8(char16ptr_t arg) {
91 // be generous... UTF16 units can expand up to 3 UTF8 units
92 size_t len = wcslen(arg);
93 // ConvertUTF16toUTF8 requires +1. Let's do that here, too, lacking
94 // knowledge of Windows internals.
95 size_t dstLen = len * 3 + 1;
96 char* s = new char[dstLen + 1]; // Another +1 for zero terminator
97 if (!s) return nullptr;
99 int written =
100 ::WideCharToMultiByte(CP_UTF8, 0, arg, len, s, dstLen, nullptr, nullptr);
101 s[written] = 0;
102 return s;
105 static void FreeAllocStrings(int argc, char** argv) {
106 while (argc) {
107 --argc;
108 delete[] argv[argc];
111 delete[] argv;
114 int wmain(int argc, WCHAR** argv) {
115 SanitizeEnvironmentVariables();
116 SetDllDirectoryW(L"");
118 // Only run this code if LauncherProcessWin.h was included beforehand, thus
119 // signalling that the hosting process should support launcher mode.
120 #if defined(mozilla_LauncherProcessWin_h)
121 mozilla::Maybe<int> launcherResult =
122 mozilla::LauncherMain(argc, argv, sAppData);
123 if (launcherResult) {
124 return launcherResult.value();
126 #endif // defined(mozilla_LauncherProcessWin_h)
128 // Remove "/prefetch:##" argument from the command line, if present. (See
129 // GeckoChildProcessHost.cpp for details.)
131 // Colons are not permitted in path-elements on Windows, so a string of this
132 // form is extremely unlikely to appear with the intent of being a legitimate
133 // path-argument.
134 for (int i = 0; i < argc; ++i) {
135 constexpr const wchar_t prefix[] = L"/prefetch:";
136 auto const cmp = wcsncmp(argv[i], prefix, ARRAYSIZE(prefix) - 1);
137 if (cmp == 0) {
138 std::copy(argv + i + 1, argv + argc, argv + i);
139 --argc;
140 break; // assume there's only one
144 char** argvConverted = new char*[argc + 1];
145 if (!argvConverted) return 127;
147 for (int i = 0; i < argc; ++i) {
148 argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
149 if (!argvConverted[i]) {
150 return 127;
153 argvConverted[argc] = nullptr;
155 // need to save argvConverted copy for later deletion.
156 char** deleteUs = new char*[argc + 1];
157 if (!deleteUs) {
158 FreeAllocStrings(argc, argvConverted);
159 return 127;
161 for (int i = 0; i < argc; i++) deleteUs[i] = argvConverted[i];
162 #ifndef XRE_WANT_ENVIRON
163 int result = main(argc, argvConverted);
164 #else
165 // Force creation of the multibyte _environ variable.
166 getenv("PATH");
167 int result = main(argc, argvConverted, _environ);
168 #endif
170 delete[] argvConverted;
171 FreeAllocStrings(argc, deleteUs);
173 return result;
176 #endif // nsWindowsWMain_cpp