Bug 1509929: Part 2 - Modify MakeCommandLine to accept an array of extra args that will be appended to the final result; r=mhowell
authorAaron Klotz <aklotz@mozilla.com>
Tue, 08 Jan 2019 00:09:52 +0000
changeset 509899 f0fdb5395e783fee406b08ca41856e1e7ab2f6cd
parent 509898 d0801e093f22a9148133a9981a45199e5c40373a
child 509900 4dceaafd23a84cefb4bcba39bdceb65a90852f06
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1509929
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1509929: Part 2 - Modify MakeCommandLine to accept an array of extra args that will be appended to the final result; r=mhowell Differential Revision: https://phabricator.services.mozilla.com/D15447
toolkit/xre/CmdLineAndEnvUtils.h
--- a/toolkit/xre/CmdLineAndEnvUtils.h
+++ b/toolkit/xre/CmdLineAndEnvUtils.h
@@ -291,40 +291,63 @@ inline wchar_t* ArgToString(wchar_t* d, 
 
   return d;
 }
 
 }  // namespace internal
 
 /**
  * Creates a command line from a list of arguments.
+ *
+ * @param argc Number of elements in |argv|
+ * @param argv Array of arguments
+ * @param aArgcExtra Number of elements in |aArgvExtra|
+ * @param aArgvExtra Optional array of arguments to be appended to the resulting
+ *                   command line after those provided by |argv|.
  */
-inline UniquePtr<wchar_t[]> MakeCommandLine(int argc, wchar_t** argv) {
+inline UniquePtr<wchar_t[]> MakeCommandLine(int argc, wchar_t** argv,
+                                            int aArgcExtra = 0,
+                                            wchar_t** aArgvExtra = nullptr) {
   int i;
   int len = 0;
 
-  // The + 1 of the last argument handles the allocation for null termination
+  // The + 1 for each argument reserves space for either a ' ' or the null
+  // terminator, depending on the position of the argument.
   for (i = 0; i < argc; ++i) {
     len += internal::ArgStrLen(argv[i]) + 1;
   }
 
+  for (i = 0; i < aArgcExtra; ++i) {
+    len += internal::ArgStrLen(aArgvExtra[i]) + 1;
+  }
+
   // Protect against callers that pass 0 arguments
   if (len == 0) {
     len = 1;
   }
 
   auto s = MakeUnique<wchar_t[]>(len);
   if (!s) {
     return s;
   }
 
+  int totalArgc = argc + aArgcExtra;
+
   wchar_t* c = s.get();
   for (i = 0; i < argc; ++i) {
     c = internal::ArgToString(c, argv[i]);
-    if (i + 1 != argc) {
+    if (i + 1 != totalArgc) {
+      *c = ' ';
+      ++c;
+    }
+  }
+
+  for (i = 0; i < aArgcExtra; ++i) {
+    c = internal::ArgToString(c, aArgvExtra[i]);
+    if (i + 1 != aArgcExtra) {
       *c = ' ';
       ++c;
     }
   }
 
   *c = '\0';
 
   return s;