Windows: ignore envvars with degenerate names

Fixes https://github.com/bazelbuild/bazel/issues/3077

Change-Id: I5f29fdec1f69bdda51fa62be695b8791a45240e9
PiperOrigin-RevId: 157559455
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index 2a769c3..3571486 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -18,7 +18,7 @@
 
 PROTO_FILES=$(ls src/main/protobuf/*.proto src/main/java/com/google/devtools/build/lib/buildeventstream/proto/*.proto)
 LIBRARY_JARS=$(find third_party -name '*.jar' | grep -Fv /javac-9-dev-r3297-4.jar | grep -Fv /javac-9-dev-4023-2.jar | grep -Fv /javac7.jar | grep -Fv JavaBuilder | grep -Fv third_party/guava | grep -Fv third_party/guava | grep -ve third_party/grpc/grpc.*jar | tr "\n" " ")
-GRPC_JAVA_VERSION=1.3.0
+GRPC_JAVA_VERSION=0.15.0
 GRPC_LIBRARY_JARS=$(find third_party/grpc -name '*.jar' | grep -e .*${GRPC_JAVA_VERSION}.*jar | tr "\n" " ")
 # Guava jars are different for JDK 7 build and JDK 8 build, we select the good
 # one based on the name (21.0-{date} for JDK7).
@@ -327,9 +327,12 @@
   local client_env=()
   # Propagate all environment variables to bootstrapped Bazel.
   # See https://stackoverflow.com/questions/41898503/loop-over-environment-variables-in-posix-sh
-  local env_vars="$(awk 'END { for (name in ENVIRON) { if(name != "_") print name; } }' </dev/null)"
+  local env_vars="$(awk 'END { for (name in ENVIRON) { if(name != "_" && name ~ /^[A-Za-z0-9_]*$/) print name; } }' </dev/null)"
   for varname in $env_vars; do
     eval value=\$$varname
+    if [ "${PLATFORM}" = "windows" ] && echo "$varname" | grep -q -i "^\(path\|tmp\|temp\|tempdir\|systemroot\)$" ; then
+      varname="$(echo "$varname" | tr [:lower:] [:upper:])"
+    fi
     if [ "${value}" ]; then
       client_env=("${client_env[@]}" --client_env="${varname}=${value}")
     fi
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 9e750c5..553682f 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -481,6 +481,18 @@
   return blaze_exit_code::SUCCESS;
 }
 
+static bool IsValidEnvName(const char* p) {
+#if defined(COMPILER_MSVC) || defined(__CYGWIN__)
+  for (; *p && *p != '='; ++p) {
+    if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') ||
+          (*p >= '0' && *p <= '9') || *p == '_')) {
+      return false;
+    }
+  }
+#endif
+  return true;
+}
+
 #if defined(COMPILER_MSVC)
 static void PreprocessEnvString(string* env_str) {
   static std::set<string> vars_to_uppercase = {"PATH", "TMP", "TEMP", "TEMPDIR",
@@ -558,8 +570,10 @@
   // Pass the client environment to the server.
   for (char** env = environ; *env != NULL; env++) {
     string env_str(*env);
-    PreprocessEnvString(&env_str);
-    command_arguments_.push_back("--client_env=" + env_str);
+    if (IsValidEnvName(*env)) {
+      PreprocessEnvString(&env_str);
+      command_arguments_.push_back("--client_env=" + env_str);
+    }
   }
   command_arguments_.push_back("--client_cwd=" + blaze::ConvertPath(cwd));