Bug 1242343 - p1. ConstructSystem32Path from LoadLibrarySystem32 - r?jimm draft
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 02 Mar 2016 16:22:00 +1100
changeset 336050 9e330da9a70eea470b986c2751e3d80f6810d90f
parent 335982 eb25b90a05c194bfd4f498ff3ffee7440f85f1cd
child 336051 d3e737d1143c58737e1dda284c4a33e2397fd777
push id11951
push usergsquelart@mozilla.com
push dateWed, 02 Mar 2016 05:22:20 +0000
reviewersjimm
bugs1242343
milestone47.0a1
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: SuAWUQf3M0
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:
@@ -142,39 +143,66 @@ public:
 typedef nsAutoRef<HKEY> nsAutoRegKey;
 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