Bug 1242343 - p1. ConstructSystem32Path from LoadLibrarySystem32 - r=jimm
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 03 Mar 2016 08:40:23 +1100
changeset 322859 56e2ad84a18f8ab89ceeba12c54d5dbea7add5d3
parent 322858 ae5567d429b03861360bde907d0203b2f1228597
child 322860 90f70787de03db68c4e82c6b99444d9100583bc5
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs1242343
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1242343 - p1. ConstructSystem32Path from LoadLibrarySystem32 - r=jimm Part 1: Refactored LoadLibrarySystem32 to expose the system32-path construction code, so it can be re-used in the following patch. MozReview-Commit-ID: J5BcI34VPnN
xpcom/base/nsWindowsHelpers.h
--- a/xpcom/base/nsWindowsHelpers.h
+++ b/xpcom/base/nsWindowsHelpers.h
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsWindowsHelpers_h
 #define nsWindowsHelpers_h
 
 #include <windows.h>
 #include "nsAutoRef.h"
 #include "nscore.h"
+#include "mozilla/Assertions.h"
 
 // ----------------------------------------------------------------------------
 // Critical Section helper class
 // ----------------------------------------------------------------------------
 
 class AutoCriticalSection
 {
 public:
@@ -218,39 +219,66 @@ typedef nsAutoRef<HRGN> nsAutoRegion;
 typedef nsAutoRef<HBITMAP> nsAutoBitmap;
 typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
 typedef nsAutoRef<HANDLE> nsAutoHandle;
 typedef nsAutoRef<HMODULE> nsModuleHandle;
 typedef nsAutoRef<DEVMODEW*> nsAutoDevMode;
 
 namespace {
 
+// Construct a path "<system32>\<aModule>". return false if the output buffer
+// is too small.
+// Note: If the system path cannot be found, or doesn't fit in the output buffer
+// with the module name, we will just ignore the system path and output the
+// module name alone;
+// this may mean using a normal search path wherever the output is used.
+bool inline
+ConstructSystem32Path(LPCWSTR aModule, WCHAR* aSystemPath, UINT aSize)
+{
+  MOZ_ASSERT(aSystemPath);
+
+  size_t fileLen = wcslen(aModule);
+  if (fileLen >= aSize) {
+    // The module name alone cannot even fit!
+    return false;
+  }
+
+  size_t systemDirLen = GetSystemDirectoryW(aSystemPath, aSize);
+
+  if (systemDirLen) {
+    if (systemDirLen < aSize - fileLen) {
+      // Make the system directory path terminate with a slash.
+      if (aSystemPath[systemDirLen - 1] != L'\\') {
+        if (systemDirLen + 1 < aSize - fileLen) {
+            aSystemPath[systemDirLen] = L'\\';
+            ++systemDirLen;
+            // No need to re-nullptr terminate.
+        } else {
+          // Couldn't fit the system path with added slash.
+          systemDirLen = 0;
+        }
+      }
+    } else {
+      // Couldn't fit the system path.
+      systemDirLen = 0;
+    }
+  }
+
+  MOZ_ASSERT(systemDirLen + fileLen < aSize);
+
+  wcsncpy(aSystemPath + systemDirLen, aModule, fileLen);
+  aSystemPath[systemDirLen + fileLen] = L'\0';
+  return true;
+}
+
 HMODULE inline
 LoadLibrarySystem32(LPCWSTR aModule)
 {
-  WCHAR systemPath[MAX_PATH + 1] = { L'\0' };
-
-  // If GetSystemPath fails we accept that we'll load the DLLs from the
-  // normal search path.
-  GetSystemDirectoryW(systemPath, MAX_PATH + 1);
-  size_t systemDirLen = wcslen(systemPath);
-
-  // Make the system directory path terminate with a slash
-  if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') {
-    systemPath[systemDirLen] = L'\\';
-    ++systemDirLen;
-    // No need to re-nullptr terminate
-  }
-
-  size_t fileLen = wcslen(aModule);
-  wcsncpy(systemPath + systemDirLen, aModule,
-          MAX_PATH - systemDirLen);
-  if (systemDirLen + fileLen <= MAX_PATH) {
-    systemPath[systemDirLen + fileLen] = L'\0';
-  } else {
-    systemPath[MAX_PATH] = L'\0';
+  WCHAR systemPath[MAX_PATH + 1];
+  if (!ConstructSystem32Path(aModule, systemPath, MAX_PATH + 1)) {
+    return NULL;
   }
   return LoadLibraryW(systemPath);
 }
 
 }
 
 #endif