Fix broken --shutdown-leaks argument handling on Windows by copying the argument out of argv rather than holding a pointer to it. b=433708 r=pavlov
authorL. David Baron <dbaron@dbaron.org>
Wed, 14 May 2008 11:57:32 -0700
changeset 15123 111c20038fa667049218d2dbc0023f54a07098b4
parent 15122 a6b0f5b2fb7dec3ab56aeb68702ab0631e5a6fd8
child 15124 3c2a38984d4c743e6076fd54e7221c3a52999884
push idunknown
push userunknown
push dateunknown
reviewerspavlov
bugs433708
milestone1.9.1a1pre
Fix broken --shutdown-leaks argument handling on Windows by copying the argument out of argv rather than holding a pointer to it. b=433708 r=pavlov
tools/trace-malloc/lib/nsTraceMalloc.c
--- a/tools/trace-malloc/lib/nsTraceMalloc.c
+++ b/tools/trace-malloc/lib/nsTraceMalloc.c
@@ -135,17 +135,20 @@ struct logfile {
 
 static char      default_buf[STARTUP_TMBUFSIZE];
 static logfile   default_logfile =
                    {-1, 0, default_buf, STARTUP_TMBUFSIZE, 0, 0, 0, NULL, NULL};
 static logfile   *logfile_list = NULL;
 static logfile   **logfile_tail = &logfile_list;
 static logfile   *logfp = &default_logfile;
 static PRLock    *tmlock = NULL;
-static char      *sdlogname = NULL; /* filename for shutdown leak log */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+static char      sdlogname[PATH_MAX] = ""; /* filename for shutdown leak log */
 
 /*
  * This enables/disables trace-malloc logging.
  *
  * It is separate from suppress_tracing so that we do not have to pay
  * the performance cost of repeated TM_TLS_GET_DATA calls when
  * trace-malloc is disabled (which is not as bad as the locking we used
  * to have).
@@ -1344,29 +1347,29 @@ static const char SDLOG_OPTION[] = "--sh
             consumed_ = 2;                                                    \
         }                                                                     \
     PR_END_MACRO
 
 PR_IMPLEMENT(int)
 NS_TraceMallocStartupArgs(int argc, char **argv)
 {
     int i, logfd = -1, consumed, logflags;
-    char *tmlogname = NULL; /* note global |sdlogname| */
+    char *tmlogname = NULL, *sdlogname_local = NULL;
 
     /*
      * Look for the --trace-malloc <logfile> option early, to avoid missing
      * early mallocs (we miss static constructors whose output overflows the
      * log file's static 16K output buffer).
      */
     for (i = 1; i < argc; i += consumed) {
         consumed = 0;
         if (SHOULD_PARSE_ARG(TMLOG_OPTION, tmlogname, argv[i]))
             PARSE_ARG(TMLOG_OPTION, tmlogname, argv, i, consumed);
-        else if (SHOULD_PARSE_ARG(SDLOG_OPTION, sdlogname, argv[i]))
-            PARSE_ARG(SDLOG_OPTION, sdlogname, argv, i, consumed);
+        else if (SHOULD_PARSE_ARG(SDLOG_OPTION, sdlogname_local, argv[i]))
+            PARSE_ARG(SDLOG_OPTION, sdlogname_local, argv, i, consumed);
 
         if (consumed) {
 #ifndef XP_WIN32 /* If we don't comment this out, it will crash Windows. */
             int j;
             /* Now remove --trace-malloc and its argument from argv. */
             argc -= consumed;
             for (j = i; j < argc; ++j)
                 argv[j] = argv[j+consumed];
@@ -1456,26 +1459,31 @@ NS_TraceMallocStartupArgs(int argc, char
                     "%s: can't create trace-malloc log named %s: %s\n",
                     argv[0], tmlogname, strerror(errno));
                 exit(1);
             }
             break;
         }
     }
 
+    if (sdlogname_local) {
+        strncpy(sdlogname, sdlogname_local, sizeof(sdlogname));
+        sdlogname[sizeof(sdlogname) - 1] = '\0';
+    }
+
     NS_TraceMallocStartup(logfd);
     return argc;
 }
 
 PR_IMPLEMENT(void)
 NS_TraceMallocShutdown(void)
 {
     logfile *fp;
 
-    if (sdlogname)
+    if (sdlogname[0])
         NS_TraceMallocDumpAllocations(sdlogname);
 
     if (tmstats.backtrace_failures) {
         fprintf(stderr,
                 "TraceMalloc backtrace failures: %lu (malloc %lu dladdr %lu)\n",
                 (unsigned long) tmstats.backtrace_failures,
                 (unsigned long) tmstats.btmalloc_failures,
                 (unsigned long) tmstats.dladdr_failures);