[8.9.0] Fix `.ar` extraction of BSD long names with NUL padding (#32592) (#32724)

This PR fixes two problems. We found them when we updated the macOS CI
image to macOS 26 with Xcode 26.

**1. Bazel cannot extract macOS static libraries.**
The macOS `ar` tool writes some member names as `#1/<len>` and puts NUL
bytes after the name, for example `__.SYMDEF SORTED\0\0\0\0`. Bazel kept
the NUL bytes in the file name, and the extraction failed with `Invalid
file path`. All macOS static libraries have this symbol table member.

Fix: `ArFunction` removes the NUL bytes at the end of entry names.

**2. `test_extract_non_readable_file_ar` fails with Xcode 26.**
The test creates its archive with the system `ar` tool. With Xcode 26,
`ar` runs `ranlib`. `ranlib` removes members that are not Mach-O files,
so the archive contains only an empty `__.SYMDEF SORTED` member.

Fix: The test writes the archive with Python. It does not use the system
`ar` tool.

Repro (Xcode 26):

```
echo hello > f.txt && ar rc f.ar f.txt && ar t f.ar   # prints only "__.SYMDEF SORTED"
```

Tests:
- Added `ArFunctionTest.testDecompressBsdLongNamesWithNulPadding`.
- Ran `test_extract_non_readable_file_ar` on a macOS 26.7.2 VM with
Xcode 26.6.

Closes #32592

Co-authored-by: Yun Peng <pcloudy@google.com>
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/ArFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/ArFunction.java
index 7d44bbf..7b92dab 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/ArFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/ArFunction.java
@@ -14,6 +14,7 @@
 
 package com.google.devtools.build.lib.bazel.repository;
 
+import com.google.common.base.CharMatcher;
 import com.google.common.io.ByteStreams;
 import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
 import com.google.devtools.build.lib.vfs.Path;
@@ -50,7 +51,9 @@
       ArArchiveInputStream arStream = new ArArchiveInputStream(decompressorStream);
       ArArchiveEntry entry;
       while ((entry = arStream.getNextArEntry()) != null) {
-        String entryName = entry.getName();
+        // BSD ar (used on macOS) pads long names ("#1/<len>") with NUL bytes, for example
+        // "__.SYMDEF SORTED\0\0\0\0". Remove the padding.
+        String entryName = CharMatcher.is('\0').trimTrailingFrom(entry.getName());
         entryName = renameFiles.getOrDefault(entryName, entryName);
         Path filePath = descriptor.destinationPath().getRelative(entryName);
         filePath.getParentDirectory().createDirectoryAndParents();
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/ArFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/ArFunctionTest.java
index 5db3857..885fa5e 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/repository/ArFunctionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/ArFunctionTest.java
@@ -15,6 +15,7 @@
 package com.google.devtools.build.lib.bazel.repository;
 
 import static com.google.common.truth.Truth.assertThat;
+import static java.nio.charset.StandardCharsets.UTF_8;
 
 import com.google.devtools.build.lib.testutil.TestConstants;
 import com.google.devtools.build.lib.testutil.TestUtils;
@@ -24,17 +25,23 @@
 import com.google.devtools.build.lib.vfs.FileSystem;
 import com.google.devtools.build.lib.vfs.JavaIoFileSystem;
 import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.util.FileSystems;
 import com.google.devtools.build.runfiles.Runfiles;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.HashMap;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
 /** Tests decompressing archives. */
 @RunWith(JUnit4.class)
 public class ArFunctionTest {
+  @Rule public TemporaryFolder folder = new TemporaryFolder();
+
   /*
    * .ar archive created with ar cr test_files.ar archived_first.txt archived_second.md
    * The files contain short UTF-8 encoded strings.
@@ -83,6 +90,49 @@
     return new ArFunction().decompress(descriptor);
   }
 
+  /**
+   * Test decompressing a BSD ar archive (as created by the macOS ar and ranlib) with long names
+   * ("#1/len") that are padded with NUL bytes.
+   */
+  @Test
+  public void testDecompressBsdLongNamesWithNulPadding() throws Exception {
+    FileSystem fs = FileSystems.getNativeFileSystem();
+    File arFile = folder.newFile("bsd.ar");
+    try (FileOutputStream out = new FileOutputStream(arFile)) {
+      out.write("!<arch>\n".getBytes(UTF_8));
+      writeBsdLongNameMember(out, "__.SYMDEF SORTED\0\0\0\0", new byte[8]);
+      writeBsdLongNameMember(out, "a_long_object_name.o\0\0\0\0", "content".getBytes(UTF_8));
+    }
+    Path arPath = fs.getPath(arFile.getAbsolutePath());
+    Path outDir = arPath.getParentDirectory().getRelative("out");
+
+    Path outputDir =
+        decompress(
+            DecompressorDescriptor.builder()
+                .setArchivePath(arPath)
+                .setDestinationPath(outDir)
+                .build());
+
+    assertThat(outputDir.getRelative("__.SYMDEF SORTED").exists()).isTrue();
+    Path objectFile = outputDir.getRelative("a_long_object_name.o");
+    assertThat(objectFile.exists()).isTrue();
+    assertThat(objectFile.getFileSize()).isEqualTo(7);
+  }
+
+  private static void writeBsdLongNameMember(FileOutputStream out, String name, byte[] data)
+      throws IOException {
+    int size = name.length() + data.length;
+    String header =
+        String.format(
+            "%-16s%-12s%-6s%-6s%-8s%-10s`\n", "#1/" + name.length(), "0", "0", "0", "644", size);
+    out.write(header.getBytes(UTF_8));
+    out.write(name.getBytes(UTF_8));
+    out.write(data);
+    if (size % 2 != 0) {
+      out.write('\n');
+    }
+  }
+
   private DecompressorDescriptor.Builder createDescriptorBuilder() throws IOException {
     // This was cribbed from TestArchiveDescriptor
     FileSystem testFS =
diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh
index 955003b..3026b57 100755
--- a/src/test/shell/bazel/starlark_repository_test.sh
+++ b/src/test/shell/bazel/starlark_repository_test.sh
@@ -3868,20 +3868,17 @@
 function test_extract_non_readable_file_ar() {
   local archive_ar="${TEST_TMPDIR}/non_readable.ar"
 
-  # Create a valid AR file, then patch the permission field to 0000
-  pushd "${TEST_TMPDIR}"
-  echo "secret ar content" > non_readable.txt
-  ar rc non_readable.ar non_readable.txt
-  # Patch the mode field (bytes 40-47 after the 8-byte magic) to "0       "
+  # Write the AR file directly. Do not use the system "ar": on macOS with
+  # Xcode 26, "ar" runs ranlib, which drops members that are not Mach-O files.
   python3 -c "
-with open('non_readable.ar', 'r+b') as f:
-    # AR magic is 8 bytes, then header starts
-    # Header format: filename(16) + mtime(12) + owner(6) + group(6) + mode(8) + size(10) + magic(2)
-    # Mode is at offset 8 + 16 + 12 + 6 + 6 = 48
-    f.seek(48)
-    f.write(b'0       ')  # 8 bytes for mode 0000 in octal
+data = b'secret ar content\\n'
+# Header: name(16) mtime(12) uid(6) gid(6) mode(8, octal) size(10) magic(2).
+header = (b'non_readable.txt'.ljust(16) + b'0'.ljust(12) + b'0'.ljust(6) +
+          b'0'.ljust(6) + b'0'.ljust(8) + str(len(data)).encode().ljust(10) +
+          b'\\x60\\n')
+with open('${archive_ar}', 'wb') as f:
+    f.write(b'!<arch>\\n' + header + data + b'\\n' * (len(data) % 2))
 "
-  popd
 
   _run_extract_test "${archive_ar}" "non_readable.txt" "secret ar content"
 }