Thread-safety of mbstowcs() and wcstombs()

Igor Liferenko igor.liferenko@gmail.com
Thu Oct 6 08:34:00 GMT 2016


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



More information about the Libc-help mailing list