Bug 1333081 - Avoid using the STL in nsXPCOMGlue.cpp to avoid allocator mismatch on Windows. r=froydnj draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 24 Jan 2017 08:44:42 +0900
changeset 465810 da46a23e42c0088219e2c98c40273a252e320cde
parent 465551 638d26081a81fe9d2ebc2ee703291e8171f0f331
child 465822 edbe919e219b7f18cc5425dec6e48dfbe5723dd4
push id42730
push userbmo:mh+mozilla@glandium.org
push dateTue, 24 Jan 2017 22:53:26 +0000
reviewersfroydnj
bugs1333081
milestone54.0a1
Bug 1333081 - Avoid using the STL in nsXPCOMGlue.cpp to avoid allocator mismatch on Windows. r=froydnj
xpcom/glue/standalone/nsXPCOMGlue.cpp
--- a/xpcom/glue/standalone/nsXPCOMGlue.cpp
+++ b/xpcom/glue/standalone/nsXPCOMGlue.cpp
@@ -8,20 +8,20 @@
 
 #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"
+#include "mozilla/UniquePtrExtensions.h"
 
 using namespace mozilla;
 
 #define XPCOM_DEPENDENT_LIBS_LIST "dependentlibs.list"
 
 #if defined(XP_WIN)
 #define READ_TEXTMODE L"rt"
 #else
@@ -390,24 +390,27 @@ GetBootstrap(const char* aXPCOMFile)
 #ifdef MOZ_GSLICE_INIT
   GSliceInit gSliceInit;
 #endif
 
   if (!aXPCOMFile) {
     return nullptr;
   }
 
-  std::string file(aXPCOMFile);
-  size_t lastSlash = file.rfind(XPCOM_FILE_PATH_SEPARATOR[0]);
-  if (lastSlash == std::string::npos) {
+  char *lastSlash = strrchr(const_cast<char *>(aXPCOMFile), XPCOM_FILE_PATH_SEPARATOR[0]);
+  if (!lastSlash) {
     return nullptr;
   }
-  file.replace(lastSlash + 1, std::string::npos, XPCOM_DLL);
+  size_t base_len = size_t(lastSlash - aXPCOMFile) + 1;
 
-  if (NS_FAILED(XPCOMGlueLoad(file.c_str()))) {
+  UniqueFreePtr<char> file(reinterpret_cast<char*>(malloc(base_len + sizeof(XPCOM_DLL))));
+  memcpy(file.get(), aXPCOMFile, base_len);
+  memcpy(file.get() + base_len, XPCOM_DLL, sizeof(XPCOM_DLL));
+
+  if (NS_FAILED(XPCOMGlueLoad(file.get()))) {
     return nullptr;
   }
 
   GetBootstrapType func = (GetBootstrapType)GetSymbol(sTop->libHandle, "XRE_GetBootstrap");
   if (!func) {
     return nullptr;
   }