This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Thread-safety of mbstowcs() and wcstombs()
- From: Igor Liferenko <igor dot liferenko at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Thu, 6 Oct 2016 15:34:10 +0700
- Subject: Thread-safety of mbstowcs() and wcstombs()
- Authentication-results: sourceware.org; auth=none
Hi all,
I have three questions:
1)
In mbstowcs.c there is this warning:
Attention: this function should NEVER be intentionally used.
The interface is completely stupid. The state is shared between
all conversion functions. You should use instead the restartable
version `mbsrtowcs'.
But in wcstombs.c there is no such warning. Why?
2)
Why in libc reference
https://www.gnu.org/software/libc/manual/html_node/Non_002dreentrant-Conversion.html
it is written that "multiple conversions at the same time (not only
when using threads) cannot be done"?
3)
I use UTF-8 external encoding and UCS-4 internal encoding. They are
both stateless.
Under these assumptions, I need to make the functions wcstombs() and
mbstowcs() thread-safe.
Below are the definitions of mbstowcs() and wcstombs() (from
glibc-2.24 sources). How these definitions should be changed to make
them thread-safe?
size_t
mbstowcs (wchar_t *pwcs, const char *s, size_t n)
{
mbstate_t state;
memset (&state, '\0', sizeof state);
return __mbsrtowcs (pwcs, &s, n, &state);
}
size_t
wcstombs (char *s, const wchar_t *pwcs, size_t n)
{
mbstate_t state;
memset (&state, '\0', sizeof state);
return __wcsrtombs (s, &pwcs, n, &state);
}
Regards,
Igor