From 65b2fe2b53fd1dd5a88e7c6d96d7c9d71f447f5c Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Wed, 1 Mar 2017 22:54:24 -0500 Subject: [PATCH] Bug 1343866 - Scope more nsCOMPtrs in XRE_XPCShellMain(); r=bsmedberg --- js/xpconnect/src/XPCShellImpl.cpp | 193 +++++++++++++++--------------- 1 file changed, 96 insertions(+), 97 deletions(-) diff --git a/js/xpconnect/src/XPCShellImpl.cpp b/js/xpconnect/src/XPCShellImpl.cpp index 152436cf0917..4823cc036aaf 100644 --- a/js/xpconnect/src/XPCShellImpl.cpp +++ b/js/xpconnect/src/XPCShellImpl.cpp @@ -1323,115 +1323,115 @@ XRE_XPCShellMain(int argc, char** argv, char** envp, printf_stderr("*** You are running in chaos test mode. See ChaosMode.h. ***\n"); } - nsCOMPtr appFile; - rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appFile)); - if (NS_FAILED(rv)) { - printf("Couldn't find application file.\n"); - return 1; - } - nsCOMPtr appDir; - rv = appFile->GetParent(getter_AddRefs(appDir)); - if (NS_FAILED(rv)) { - printf("Couldn't get application directory.\n"); - return 1; - } - - XPCShellDirProvider dirprovider; - - dirprovider.SetAppFile(appFile); - - nsCOMPtr greDir; - if (argc > 1 && !strcmp(argv[1], "-g")) { - if (argc < 3) - return usage(); - - rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(greDir)); + { // Start scoping nsCOMPtrs + nsCOMPtr appFile; + rv = XRE_GetBinaryPath(argv[0], getter_AddRefs(appFile)); if (NS_FAILED(rv)) { - printf("Couldn't use given GRE dir.\n"); + printf("Couldn't find application file.\n"); + return 1; + } + nsCOMPtr appDir; + rv = appFile->GetParent(getter_AddRefs(appDir)); + if (NS_FAILED(rv)) { + printf("Couldn't get application directory.\n"); return 1; } - dirprovider.SetGREDirs(greDir); + XPCShellDirProvider dirprovider; - argc -= 2; - argv += 2; - } else { + dirprovider.SetAppFile(appFile); + + nsCOMPtr greDir; + if (argc > 1 && !strcmp(argv[1], "-g")) { + if (argc < 3) + return usage(); + + rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(greDir)); + if (NS_FAILED(rv)) { + printf("Couldn't use given GRE dir.\n"); + return 1; + } + + dirprovider.SetGREDirs(greDir); + + argc -= 2; + argv += 2; + } else { #ifdef XP_MACOSX - // On OSX, the GreD needs to point to Contents/Resources in the .app - // bundle. Libraries will be loaded at a relative path to GreD, i.e. - // ../MacOS. - nsCOMPtr tmpDir; - XRE_GetFileFromPath(argv[0], getter_AddRefs(greDir)); - greDir->GetParent(getter_AddRefs(tmpDir)); - tmpDir->Clone(getter_AddRefs(greDir)); - tmpDir->SetNativeLeafName(NS_LITERAL_CSTRING("Resources")); - bool dirExists = false; - tmpDir->Exists(&dirExists); - if (dirExists) { - greDir = tmpDir.forget(); - } - dirprovider.SetGREDirs(greDir); + // On OSX, the GreD needs to point to Contents/Resources in the .app + // bundle. Libraries will be loaded at a relative path to GreD, i.e. + // ../MacOS. + nsCOMPtr tmpDir; + XRE_GetFileFromPath(argv[0], getter_AddRefs(greDir)); + greDir->GetParent(getter_AddRefs(tmpDir)); + tmpDir->Clone(getter_AddRefs(greDir)); + tmpDir->SetNativeLeafName(NS_LITERAL_CSTRING("Resources")); + bool dirExists = false; + tmpDir->Exists(&dirExists); + if (dirExists) { + greDir = tmpDir.forget(); + } + dirprovider.SetGREDirs(greDir); #else - nsAutoString workingDir; - if (!GetCurrentWorkingDirectory(workingDir)) { - printf("GetCurrentWorkingDirectory failed.\n"); - return 1; - } - rv = NS_NewLocalFile(workingDir, true, getter_AddRefs(greDir)); - if (NS_FAILED(rv)) { - printf("NS_NewLocalFile failed.\n"); - return 1; - } + nsAutoString workingDir; + if (!GetCurrentWorkingDirectory(workingDir)) { + printf("GetCurrentWorkingDirectory failed.\n"); + return 1; + } + rv = NS_NewLocalFile(workingDir, true, getter_AddRefs(greDir)); + if (NS_FAILED(rv)) { + printf("NS_NewLocalFile failed.\n"); + return 1; + } #endif - } - - if (argc > 1 && !strcmp(argv[1], "-a")) { - if (argc < 3) - return usage(); - - nsCOMPtr dir; - rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(dir)); - if (NS_SUCCEEDED(rv)) { - appDir = do_QueryInterface(dir, &rv); - dirprovider.SetAppDir(appDir); } - if (NS_FAILED(rv)) { - printf("Couldn't use given appdir.\n"); - return 1; + + if (argc > 1 && !strcmp(argv[1], "-a")) { + if (argc < 3) + return usage(); + + nsCOMPtr dir; + rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(dir)); + if (NS_SUCCEEDED(rv)) { + appDir = do_QueryInterface(dir, &rv); + dirprovider.SetAppDir(appDir); + } + if (NS_FAILED(rv)) { + printf("Couldn't use given appdir.\n"); + return 1; + } + argc -= 2; + argv += 2; } - argc -= 2; - argv += 2; - } - while (argc > 1 && !strcmp(argv[1], "-r")) { - if (argc < 3) - return usage(); + while (argc > 1 && !strcmp(argv[1], "-r")) { + if (argc < 3) + return usage(); - nsCOMPtr lf; - rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(lf)); - if (NS_FAILED(rv)) { - printf("Couldn't get manifest file.\n"); - return 1; + nsCOMPtr lf; + rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(lf)); + if (NS_FAILED(rv)) { + printf("Couldn't get manifest file.\n"); + return 1; + } + XRE_AddManifestLocation(NS_APP_LOCATION, lf); + + argc -= 2; + argv += 2; } - XRE_AddManifestLocation(NS_APP_LOCATION, lf); - - argc -= 2; - argv += 2; - } #ifdef MOZ_CRASHREPORTER - const char* val = getenv("MOZ_CRASHREPORTER"); - if (val && *val) { - rv = CrashReporter::SetExceptionHandler(greDir, true); - if (NS_FAILED(rv)) { - printf("CrashReporter::SetExceptionHandler failed!\n"); - return 1; + const char* val = getenv("MOZ_CRASHREPORTER"); + if (val && *val) { + rv = CrashReporter::SetExceptionHandler(greDir, true); + if (NS_FAILED(rv)) { + printf("CrashReporter::SetExceptionHandler failed!\n"); + return 1; + } + MOZ_ASSERT(CrashReporter::GetEnabled()); } - MOZ_ASSERT(CrashReporter::GetEnabled()); - } #endif - { if (argc > 1 && !strcmp(argv[1], "--greomni")) { nsCOMPtr greOmni; nsCOMPtr appOmni; @@ -1626,6 +1626,11 @@ XRE_XPCShellMain(int argc, char** argv, char** envp, JS_GC(cx); } JS_GC(cx); + + dirprovider.ClearGREDirs(); + dirprovider.ClearAppDir(); + dirprovider.ClearPluginDir(); + dirprovider.ClearAppFile(); } // this scopes the nsCOMPtrs if (!XRE_ShutdownTestShell()) @@ -1643,12 +1648,6 @@ XRE_XPCShellMain(int argc, char** argv, char** envp, #endif telStats = nullptr; - appDir = nullptr; - appFile = nullptr; - dirprovider.ClearGREDirs(); - dirprovider.ClearAppDir(); - dirprovider.ClearPluginDir(); - dirprovider.ClearAppFile(); #ifdef MOZ_CRASHREPORTER // Shut down the crashreporter service to prevent leaking some strings it holds.