Bug 1332523 - Make GetBootstrap take the path to an arbitrary file next to libxul. r=bsmedberg draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 13 Jan 2017 09:27:39 +0900
changeset 464391 bfafcb69e2d80b6676f5b97a8189e85df06a2633
parent 464390 da2c2ad5833de14f4c16c3c176c28549add89669
child 464392 bd8cbec06da8600fce06d33b8ff954283c72c7f0
child 464393 9eecec222d7e4e41e3867acc632a06f901478d36
push id42348
push userbmo:mh+mozilla@glandium.org
push dateFri, 20 Jan 2017 22:46:42 +0000
reviewersbsmedberg
bugs1332523
milestone53.0a1
Bug 1332523 - Make GetBootstrap take the path to an arbitrary file next to libxul. r=bsmedberg The FileExists check can be removed because the code loading the library will handle that case already.
browser/app/nsBrowserApp.cpp
xpcom/glue/standalone/nsXPCOMGlue.cpp
--- a/browser/app/nsBrowserApp.cpp
+++ b/browser/app/nsBrowserApp.cpp
@@ -236,52 +236,27 @@ static int do_main(int argc, char* argv[
 #ifdef LIBFUZZER
   if (getenv("LIBFUZZER"))
     gBootstrap->XRE_LibFuzzerSetMain(argc, argv, libfuzzer_main);
 #endif
 
   return gBootstrap->XRE_main(argc, argv, config);
 }
 
-static bool
-FileExists(const char *path)
-{
-#ifdef XP_WIN
-  wchar_t wideDir[MAX_PATH];
-  MultiByteToWideChar(CP_UTF8, 0, path, -1, wideDir, MAX_PATH);
-  DWORD fileAttrs = GetFileAttributesW(wideDir);
-  return fileAttrs != INVALID_FILE_ATTRIBUTES;
-#else
-  return access(path, R_OK) == 0;
-#endif
-}
-
 static nsresult
 InitXPCOMGlue(const char *argv0)
 {
   char exePath[MAXPATHLEN];
 
   nsresult rv = mozilla::BinaryPath::Get(argv0, exePath);
   if (NS_FAILED(rv)) {
     Output("Couldn't find the application directory.\n");
     return rv;
   }
 
-  char *lastSlash = strrchr(exePath, XPCOM_FILE_PATH_SEPARATOR[0]);
-  if (!lastSlash ||
-      (size_t(lastSlash - exePath) > MAXPATHLEN - sizeof(XPCOM_DLL) - 1))
-    return NS_ERROR_FAILURE;
-
-  strcpy(lastSlash + 1, XPCOM_DLL);
-
-  if (!FileExists(exePath)) {
-    Output("Could not find the Mozilla runtime.\n");
-    return NS_ERROR_FAILURE;
-  }
-
   gBootstrap = mozilla::GetBootstrap(exePath);
   if (!gBootstrap) {
     Output("Couldn't load XPCOM.\n");
     return NS_ERROR_FAILURE;
   }
 
   // This will set this thread as the main thread.
   gBootstrap->NS_LogInit();
--- a/xpcom/glue/standalone/nsXPCOMGlue.cpp
+++ b/xpcom/glue/standalone/nsXPCOMGlue.cpp
@@ -8,16 +8,17 @@
 
 #include "nspr.h"
 #include "nsDebug.h"
 #include "nsIServiceManager.h"
 #include "nsXPCOMPrivate.h"
 #include "nsCOMPtr.h"
 #include <stdlib.h>
 #include <stdio.h>
+#include <string>
 
 #include "mozilla/FileUtils.h"
 #include "mozilla/Sprintf.h"
 
 using namespace mozilla;
 
 #define XPCOM_DEPENDENT_LIBS_LIST "dependentlibs.list"
 
@@ -386,20 +387,27 @@ namespace mozilla {
 Bootstrap::UniquePtr
 GetBootstrap(const char* aXPCOMFile)
 {
 #ifdef MOZ_GSLICE_INIT
   GSliceInit gSliceInit;
 #endif
 
   if (!aXPCOMFile) {
-    aXPCOMFile = XPCOM_DLL;
+    return nullptr;
   }
 
-  if (NS_FAILED(XPCOMGlueLoad(aXPCOMFile))) {
+  std::string file(aXPCOMFile);
+  size_t lastSlash = file.rfind(XPCOM_FILE_PATH_SEPARATOR[0]);
+  if (lastSlash == std::string::npos) {
+    return nullptr;
+  }
+  file.replace(lastSlash + 1, std::string::npos, XPCOM_DLL);
+
+  if (NS_FAILED(XPCOMGlueLoad(file.c_str()))) {
     return nullptr;
   }
 
   GetBootstrapType func = (GetBootstrapType)GetSymbol(sTop->libHandle, "XRE_GetBootstrap");
   if (!func) {
     return nullptr;
   }