Store invocation_policy as std::string instead of raw pointer.

Fixes crashes when parsing the policy with some STL implementations.

Note that to keep the existing semantics (--invocation_policy can only be
specified once), we have to add an extra boolean to see if the policy had been
set; we can't just use absl::optional because Bazel has not yet adopted Abseil.

RELNOTES: none.
PiperOrigin-RevId: 230962639
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 10dd0c9..e62b821 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -608,9 +608,8 @@
   }
 
   // Pass in invocation policy as a startup argument for batch mode only.
-  if (globals->options->batch && globals->options->invocation_policy != NULL &&
-      strlen(globals->options->invocation_policy) > 0) {
-    result.push_back(string("--invocation_policy=") +
+  if (globals->options->batch && !globals->options->invocation_policy.empty()) {
+    result.push_back("--invocation_policy=" +
                      globals->options->invocation_policy);
   }
 
@@ -1868,8 +1867,7 @@
   for (const string &arg : arg_vector) {
     request.add_arg(arg);
   }
-  if (globals->options->invocation_policy != NULL &&
-      strlen(globals->options->invocation_policy) > 0) {
+  if (!globals->options->invocation_policy.empty()) {
     request.set_invocation_policy(globals->options->invocation_policy);
   }
 
diff --git a/src/main/cpp/startup_options.cc b/src/main/cpp/startup_options.cc
index bbf95e2..c259bb6 100644
--- a/src/main/cpp/startup_options.cc
+++ b/src/main/cpp/startup_options.cc
@@ -88,7 +88,7 @@
       fatal_event_bus_exceptions(false),
       command_port(0),
       connect_timeout_secs(30),
-      invocation_policy(NULL),
+      have_invocation_policy_(false),
       client_debug(false),
       java_logging_formatter(
           "com.google.devtools.build.lib.util.SingleLineFormatter"),
@@ -373,7 +373,8 @@
     option_sources["command_port"] = rcfile;
   } else if ((value = GetUnaryOption(arg, next_arg, "--invocation_policy")) !=
              NULL) {
-    if (invocation_policy == NULL) {
+    if (!have_invocation_policy_) {
+      have_invocation_policy_ = true;
       invocation_policy = value;
       option_sources["invocation_policy"] = rcfile;
     } else {
diff --git a/src/main/cpp/startup_options.h b/src/main/cpp/startup_options.h
index 10492b7..07f641c 100644
--- a/src/main/cpp/startup_options.h
+++ b/src/main/cpp/startup_options.h
@@ -285,8 +285,10 @@
   // Connection timeout for each gRPC connection attempt.
   int connect_timeout_secs;
 
-  // Invocation policy proto. May be NULL.
-  const char *invocation_policy;
+  // Invocation policy proto, or an empty string.
+  std::string invocation_policy;
+  // Invocation policy can only be specified once.
+  bool have_invocation_policy_;
 
   // Whether to output addition debugging information in the client.
   bool client_debug;