Repository: bazelbuild/bazel
Issue: g++ fpermissive compilation error for strdupa on musl when buiding from source
Labels: P3
type: support / not a bug (process)
team-OSS
help wanted

### Description of the bug:


g++ will report errors as follows:

```
In file included from src/main/tools/linux-sandbox-pid1.cc:34:
src/main/tools/linux-sandbox-pid1.cc: In function ‘int CreateTarget(const char*, bool)’:
src/main/tools/linux-sandbox-pid1.cc:149:28: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
  149 |   if (CreateTarget(dirname(strdupa(path)), true) < 0) {
      |                            ^
      |                            |
      |                            void*
In file included from src/main/tools/linux-sandbox-pid1.cc:34:
/include/string.h:33:15: note:   initializing argument 1 of ‘char* strcpy(char*, const char*)’
   33 | char *strcpy (char *__restrict, const char *__restrict);
      |               ^~~~~~~~~~~~~~~~
In file included from src/main/tools/linux-sandbox-pid1.cc:67:
src/main/tools/linux-sandbox-pid1.cc:150:36: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
  150 |     DIE("CreateTarget %s", dirname(strdupa(path)));
      |                                    ^
      |                                    |
      |                                    void*
./src/main/tools/logging.h:36:51: note: in definition of macro ‘DIE’
   36 |     fprintf(stderr, __FILE__ ":" S__LINE__ ": \"" __VA_ARGS__); \
      |                                                   ^~~~~~~~~~~
In file included from src/main/tools/linux-sandbox-pid1.cc:34:
/include/string.h:33:15: note:   initializing argument 1 of ‘char* strcpy(char*, const char*)’
   33 | char *strcpy (char *__restrict, const char *__restrict);
```

I've sent patches to musl, ref [this](https://www.openwall.com/lists/musl/2022/06/22/1). Despite whether musl will accept the patch, I agreed with the author of musl that using strdupa is probably not a good idea. And we have other replacement.

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.


Build bazel 5.2.0 on musl by:

```
curl -O -L https://github.com/bazelbuild/bazel/releases/download/5.2.0/bazel-5.2.0-dist.zip
7z x bazel-5.2.0.zip
EXTRA_BAZEL_ARGS="--tool_java_runtime_version=local_jdk" ./compile.sh
```

You could try this on an docker alpine linux image.

### Which operating system are you running Bazel on?


linux with musl libc

### What is the output of `bazel info release`?


_No response_

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.


_No response_

### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?


_No response_

### Have you found anything relevant by searching the web?

_No response_

### Any other information, logs, or outputs that you want to share?

_No response_

Issue comments:
Hello @xhebox, Could you please provide complete steps to reproduce the above issue at our end. Thanks! 
> Hello @xhebox, Could you please provide complete steps to reproduce the above issue at our end. Thanks!

Edited, it really needs nothing more than normal building instructions. You could try this on alpine linux with docker. I am on my own custom linux distro with musl libc.
I will send a patch removing the strdupa, since there is exactly one usage in whole of bazel.
@bazel-io flag
@bazel-io fork 5.3.0

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/tools/linux-sandbox-pid1.cc b/src/main/tools/linux-sandbox-pid1.cc
index b6f4f3d665a758..9ec785d03e8623 100644
--- a/src/main/tools/linux-sandbox-pid1.cc
+++ b/src/main/tools/linux-sandbox-pid1.cc
@@ -146,8 +146,17 @@ static int CreateTarget(const char *path, bool is_directory) {
   }
 
   // Create the parent directory.
-  if (CreateTarget(dirname(strdupa(path)), true) < 0) {
-    DIE("CreateTarget %s", dirname(strdupa(path)));
+  {
+    char *buf, *dir;
+
+    if (!(buf = strdup(path))) DIE("strdup");
+
+    dir = dirname(buf);
+    if (CreateTarget(dir, true) < 0) {
+      DIE("CreateTarget %s", dir);
+    }
+
+    free(buf);
   }
 
   if (is_directory) {


Test patch: