glibc/wcsmbs/c32rtomb.c

33 lines
1.0 KiB
C
Raw Normal View History

Stop c32rtomb and mbrtoc32 aliasing wcrtomb and mbrtowc (bug 23793). glibc does: /* There should be no difference between the UTF-32 handling required by c32rtomb and the wchar_t handling which has long since been implemented in wcrtomb. */ weak_alias (__wcrtomb, c32rtomb) /* There should be no difference between the UTF-32 handling required by mbrtoc32 and the wchar_t handling which has long since been implemented in mbrtowc. */ weak_alias (__mbrtowc, mbrtoc32) The reasoning in those comments to justify those aliases is incorrect: ISO C requires that, for the case of a NULL mbstate_t* being passed, each function has its *own* internal static mbstate_t. Thus a program must be able to use both wcrtomb and c32rtomb at the same time with each keeping its own separate state, and likewise for mbrtowc and mbrtoc32. This patch duly sets up separarate char32_t function that wrap the wchar_t ones. Note that the included test only covers the mbrtoc32 / mbrtowc pair. While I think the change made is logically correct for c32rtomb / wcrtomb as well, I'm not sure we have a locale with a suitable state-dependent multibyte encoding for testing that part of the change. Tested for x86_64. [BZ #23793] * wcsmbs/c32rtomb.c: New file. * wcsmbs/mbrtoc32.c: Likewise. * wcsmbs/tst-c32-state.c: Likewise. * wcsmbs/mbrtowc.c (mbrtoc32): Do not define as alias. * wcsmbs/wcrtomb.c (c32rtomb): Likewise. * wcsmbs/Makefile (routines): Add mbrtoc32 and c32rtomb. (tests): Add tst-c32-state. [$(run-built-tests) = yes] ($(objpfx)tst-c32-state.out): Depend on $(gen-locales).
2018-10-22 14:52:14 +00:00
/* Convert char32_t to multibyte.
Copyright (C) 2018-2024 Free Software Foundation, Inc.
Stop c32rtomb and mbrtoc32 aliasing wcrtomb and mbrtowc (bug 23793). glibc does: /* There should be no difference between the UTF-32 handling required by c32rtomb and the wchar_t handling which has long since been implemented in wcrtomb. */ weak_alias (__wcrtomb, c32rtomb) /* There should be no difference between the UTF-32 handling required by mbrtoc32 and the wchar_t handling which has long since been implemented in mbrtowc. */ weak_alias (__mbrtowc, mbrtoc32) The reasoning in those comments to justify those aliases is incorrect: ISO C requires that, for the case of a NULL mbstate_t* being passed, each function has its *own* internal static mbstate_t. Thus a program must be able to use both wcrtomb and c32rtomb at the same time with each keeping its own separate state, and likewise for mbrtowc and mbrtoc32. This patch duly sets up separarate char32_t function that wrap the wchar_t ones. Note that the included test only covers the mbrtoc32 / mbrtowc pair. While I think the change made is logically correct for c32rtomb / wcrtomb as well, I'm not sure we have a locale with a suitable state-dependent multibyte encoding for testing that part of the change. Tested for x86_64. [BZ #23793] * wcsmbs/c32rtomb.c: New file. * wcsmbs/mbrtoc32.c: Likewise. * wcsmbs/tst-c32-state.c: Likewise. * wcsmbs/mbrtowc.c (mbrtoc32): Do not define as alias. * wcsmbs/wcrtomb.c (c32rtomb): Likewise. * wcsmbs/Makefile (routines): Add mbrtoc32 and c32rtomb. (tests): Add tst-c32-state. [$(run-built-tests) = yes] ($(objpfx)tst-c32-state.out): Depend on $(gen-locales).
2018-10-22 14:52:14 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
Stop c32rtomb and mbrtoc32 aliasing wcrtomb and mbrtowc (bug 23793). glibc does: /* There should be no difference between the UTF-32 handling required by c32rtomb and the wchar_t handling which has long since been implemented in wcrtomb. */ weak_alias (__wcrtomb, c32rtomb) /* There should be no difference between the UTF-32 handling required by mbrtoc32 and the wchar_t handling which has long since been implemented in mbrtowc. */ weak_alias (__mbrtowc, mbrtoc32) The reasoning in those comments to justify those aliases is incorrect: ISO C requires that, for the case of a NULL mbstate_t* being passed, each function has its *own* internal static mbstate_t. Thus a program must be able to use both wcrtomb and c32rtomb at the same time with each keeping its own separate state, and likewise for mbrtowc and mbrtoc32. This patch duly sets up separarate char32_t function that wrap the wchar_t ones. Note that the included test only covers the mbrtoc32 / mbrtowc pair. While I think the change made is logically correct for c32rtomb / wcrtomb as well, I'm not sure we have a locale with a suitable state-dependent multibyte encoding for testing that part of the change. Tested for x86_64. [BZ #23793] * wcsmbs/c32rtomb.c: New file. * wcsmbs/mbrtoc32.c: Likewise. * wcsmbs/tst-c32-state.c: Likewise. * wcsmbs/mbrtowc.c (mbrtoc32): Do not define as alias. * wcsmbs/wcrtomb.c (c32rtomb): Likewise. * wcsmbs/Makefile (routines): Add mbrtoc32 and c32rtomb. (tests): Add tst-c32-state. [$(run-built-tests) = yes] ($(objpfx)tst-c32-state.out): Depend on $(gen-locales).
2018-10-22 14:52:14 +00:00
#include <uchar.h>
#include <wchar.h>
/* This is the private state used if PS is NULL. */
static mbstate_t state;
size_t
c32rtomb (char *s, char32_t c32, mbstate_t *ps)
{
if (ps == NULL)
ps = &state;
return wcrtomb (s, c32, ps);
}