Does wcsdupa exist?

Google found web pages that say that strdupa exists but only in GNU C library, but I don't find wcsdupa anywhere.

Does wcsdupa exist?

strdupa invokes strlen to get the length of the input string and then it invokes alloca to create a buffer on the stack frame large enough to strcpy the input string to this buffer including the null terminator '\0' character and then returns the address of this buffer.

strdup does exactly the same thing as what strdupa does, but the only difference is that strdup invokes malloc instead of alloca, which creates the buffer on the heap instead on the stack frame.

As a result the programmer that invoked strdupa does not need and mustn't invoke free to deallocate the memory whose address was returned by strdupa, because this memory will be freed once the function that invoked strdupa returns.

But memory whose address was returned by strdup won't be deallocated until either free was invoked on it or the process that allocated this memory terminates or exits.

wcsdup also exists and does exactly the same thing as strdup does, but instead of invoking strlen, wcsdup invokes wcslen and instead of invoking strcpy, wcsdup invokes wcscpy.

Also when invoking malloc, wcsdup multiplies by sizeof(wchar_t) the integer returned by wcslen before passing it to malloc.

As I said earlier, I don't see anywhere wcsdupa and I don't know why.

When I saw strdup, strdupa and wcsdup, I also expected to see wcsdupa - a macro that does exactly the same thing as what strdupa does, i.e. invokes alloca instead of malloc and invokes the string functions that wcsdup invokes, i.e. wcslen and wcscpy.

I think that wcsdupa doesn't really exist, but I want to make sure that what I think is true.

If what I am thinking is wrong and wcsdupa already exists then where is it?
Last edited on
If it doesn't show up on Google then it doesn't exist. The question is why? It could be that since the 'w' functions were created later than the normal ones, maybe by that point they had decided that using alloca was a bad idea and didn't bother making that version. The use of alloca is generally discouraged since there's no proper error handling; if you overflow the stack your program crashes. At least with malloc you have a chance to recover (save data or whatever).
I think id doesn't exist, and perhaps it shouldn't.

Just because Windows and POSIX can grow their stacks dynamically, doesn't mean you should be using the stack this way, especially in libraries where you don't have context.

I also think it was a mistake for C99 to support dynamic arrays using alloca() for similar reasons. I believe dmr wasn't happy either, but accepted he should have paid more attention to the proposal.
GNU libc is opensource, you can submit a patch that introduces wcsdupa to string.h. I wouldn't count on its acceptance, though, seeing how once liberal use of strdupa in glibc sources led to vulnerabilities that are slowly getting discovered and fixed (such as https://bugzilla.redhat.com/show_bug.cgi?id=1321866 - that bug sat there for 20 years!)
Understood.
Topic archived. No new replies allowed.