mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Problem with strdupa in C++ code
@ 2022-04-12  1:06 Wolf
  2022-04-12 16:15 ` Markus Wichmann
  0 siblings, 1 reply; 3+ messages in thread
From: Wolf @ 2022-04-12  1:06 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 1585 bytes --]

Hello,

I'm trying to compile C++ project in Alpine (so with musl) and I'm
hitting following error:

    In file included from /usr/include/fortify/string.h:22,
                     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 /usr/include/fortify/string.h:22,
                     from src/main/tools/linux-sandbox-pid1.cc:34:
    /usr/include/string.h:31:15: note:   initializing argument 1 of 'char* strcpy(char*, const char*)'
       31 | char *strcpy (char *__restrict, const char *__restrict);
          |               ^~~~~~~~~~~~~~~~
    
If my reading for musl's code is correct, this is actually a bug in the
header file, since the declarations are:

    void *alloca(size_t);
    #define	strdupa(x)	strcpy(alloca(strlen(x)+1),x)

As far as I know, there is no automatic conversion from void* to char*
in C++. Should this be fixed by turning the define into

    #define	strdupa(x)	strcpy((char*)alloca(strlen(x)+1),x)

? I think that should be both valid C and C++.

Best regards,
Tomas Volf

-- 
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Problem with strdupa in C++ code
  2022-04-12  1:06 [musl] Problem with strdupa in C++ code Wolf
@ 2022-04-12 16:15 ` Markus Wichmann
  2022-04-12 16:24   ` Joakim Sindholt
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Wichmann @ 2022-04-12 16:15 UTC (permalink / raw)
  To: musl

Hi all,

so I tried to find some authority on this, but per the manpage,
strdupa() is a GNU extension. So I looked up the implementation in
glibc, which is near enough the only authority on whether this is
supposed to work in C++, and they do add the casts necessary to make it
work. Although they massively overcomplicate the matter with a statement
expression for God knows what reason.

So I guess we should add the cast, too. I see nothing obviously wrong
with the suggested replacement.

Ciao,
Markus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [musl] Problem with strdupa in C++ code
  2022-04-12 16:15 ` Markus Wichmann
@ 2022-04-12 16:24   ` Joakim Sindholt
  0 siblings, 0 replies; 3+ messages in thread
From: Joakim Sindholt @ 2022-04-12 16:24 UTC (permalink / raw)
  To: musl

On Tue, 12 Apr 2022 18:15:59 +0200, Markus Wichmann <nullplan@gmx.net> wrote:
> Although they massively overcomplicate the matter with a statement
> expression for God knows what reason.

To avoid referencing the argument twice, so you can strdupa(s++).

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-12 16:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-12  1:06 [musl] Problem with strdupa in C++ code Wolf
2022-04-12 16:15 ` Markus Wichmann
2022-04-12 16:24   ` Joakim Sindholt

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).