Bug 1332523 - Add BinaryPath::Get variant that returns a UniquePtr instead of filling a stack buffer. r=bsmedberg draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 13 Jan 2017 09:40:54 +0900
changeset 464393 9eecec222d7e4e41e3867acc632a06f901478d36
parent 464391 bfafcb69e2d80b6676f5b97a8189e85df06a2633
child 542916 85e7e8f95b5369f999c5ad341b9596f73690dd0d
push id42349
push userbmo:mh+mozilla@glandium.org
push dateFri, 20 Jan 2017 23:02:51 +0000
reviewersbsmedberg
bugs1332523
milestone53.0a1
Bug 1332523 - Add BinaryPath::Get variant that returns a UniquePtr instead of filling a stack buffer. r=bsmedberg
browser/app/nsBrowserApp.cpp
xpcom/build/BinaryPath.h
--- a/browser/app/nsBrowserApp.cpp
+++ b/browser/app/nsBrowserApp.cpp
@@ -239,25 +239,23 @@ static int do_main(int argc, char* argv[
 #endif
 
   return gBootstrap->XRE_main(argc, argv, config);
 }
 
 static nsresult
 InitXPCOMGlue(const char *argv0)
 {
-  char exePath[MAXPATHLEN];
-
-  nsresult rv = mozilla::BinaryPath::Get(argv0, exePath);
-  if (NS_FAILED(rv)) {
+  UniqueFreePtr<char> exePath = BinaryPath::Get(argv0);
+  if (!exePath) {
     Output("Couldn't find the application directory.\n");
-    return rv;
+    return NS_ERROR_FAILURE;
   }
 
-  gBootstrap = mozilla::GetBootstrap(exePath);
+  gBootstrap = mozilla::GetBootstrap(exePath.get());
   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/build/BinaryPath.h
+++ b/xpcom/build/BinaryPath.h
@@ -11,16 +11,18 @@
 #ifdef XP_WIN
 #include <windows.h>
 #elif defined(XP_MACOSX)
 #include <CoreFoundation/CoreFoundation.h>
 #elif defined(XP_UNIX)
 #include <sys/stat.h>
 #include <string.h>
 #endif
+#include "mozilla/UniquePtr.h"
+#include "mozilla/UniquePtrExtensions.h"
 
 namespace mozilla {
 
 class BinaryPath
 {
 public:
 #ifdef XP_WIN
   static nsresult Get(const char* argv0, char aResult[MAXPATHLEN])
@@ -150,16 +152,27 @@ private:
     return NS_ERROR_FAILURE;
   }
 
 #else
 #error Oops, you need platform-specific code here
 #endif
 
 public:
+  static UniqueFreePtr<char> Get(const char *aArgv0)
+  {
+    char path[MAXPATHLEN];
+    if (NS_FAILED(Get(aArgv0, path))) {
+      return nullptr;
+    }
+    UniqueFreePtr<char> result;
+    result.reset(strdup(path));
+    return result;
+  }
+
   static nsresult GetFile(const char* aArgv0, nsIFile** aResult)
   {
     nsCOMPtr<nsIFile> lf;
 #ifdef XP_WIN
     wchar_t exePath[MAXPATHLEN];
     nsresult rv = GetW(aArgv0, exePath);
 #else
     char exePath[MAXPATHLEN];