Fix resource unlimiting on macOS when the hard limit is above allowed max.

On macOS, it is possible for certain hard limits to be set to a value that
is higher than the configured kernel limit for such resources.  We were
already handling "unlimited" correctly by capping this to the kernel setting
but we did not account for actual values higher than the kernel setting.
Fix this now by capping the value in all cases.

You can imagine doing "ulimit -H -n 2048" and *then* lowering the kernel
max to a value below 2048, in which caseI'd expect the reported hard rlimit
to have also been lowered, but it isn't, so we must cope with that.  Or you
can also imagine setting the hard limit to a value higher than the
per-process kernel limit but lower than the total aggregate limit, which is
only possible as root.

Should fix https://github.com/bazelbuild/bazel/issues/6776.

Tested: I have manually tested this and it seems to work, but I cannot add
a test.  Adding a test would involve administrator privileges and is hard.

RELNOTES: None.
PiperOrigin-RevId: 233595690
diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc
index 1398efb..2407d9d 100644
--- a/src/main/cpp/blaze_util_posix.cc
+++ b/src/main/cpp/blaze_util_posix.cc
@@ -796,16 +796,18 @@
     return true;
   }
 
-  rl.rlim_cur = rl.rlim_max;
-  if (rl.rlim_cur == RLIM_INFINITY && !allow_infinity) {
-    const rlim_t explicit_limit = GetExplicitSystemLimit(resource);
-    if (explicit_limit <= 0) {
-      // If not implemented (-1) or on an error (0), do nothing and try to
-      // increase the soft limit to the hard one. This might fail, but it's good
-      // to try anyway.
-      assert(rl.rlim_cur == rl.rlim_max);
-    } else {
+  const rlim_t explicit_limit = GetExplicitSystemLimit(resource);
+  if (explicit_limit <= 0) {
+    // If not implemented (-1) or on an error (0), do nothing and try to
+    // increase the soft limit to the hard one. This might fail, but it's good
+    // to try anyway.
+    rl.rlim_cur = rl.rlim_max;
+  } else {
+    if ((rl.rlim_max == RLIM_INFINITY && !allow_infinity) ||
+       rl.rlim_max > explicit_limit) {
       rl.rlim_cur = explicit_limit;
+    } else {
+      rl.rlim_cur = rl.rlim_max;
     }
   }