about summary refs log tree commit diff
path: root/wcsmbs/tst-c32-state.c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2018-10-22 14:52:14 +0000
committerJoseph Myers <joseph@codesourcery.com>2018-10-22 14:52:14 +0000
commitbe8ff03f92df08055ac5f15f1b65b24026a683a5 (patch)
treeec40bc97f79dc838435cd541cab97f3cc19e38d5 /wcsmbs/tst-c32-state.c
parent2dd8e58cc533ee840d37725b11bc0dc0308a5dc0 (diff)
downloadglibc-be8ff03f92df08055ac5f15f1b65b24026a683a5.tar.gz
glibc-be8ff03f92df08055ac5f15f1b65b24026a683a5.tar.xz
glibc-be8ff03f92df08055ac5f15f1b65b24026a683a5.zip
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).
Diffstat (limited to 'wcsmbs/tst-c32-state.c')
-rw-r--r--wcsmbs/tst-c32-state.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/wcsmbs/tst-c32-state.c b/wcsmbs/tst-c32-state.c
new file mode 100644
index 0000000000..68f646c801
--- /dev/null
+++ b/wcsmbs/tst-c32-state.c
@@ -0,0 +1,44 @@
+/* Test mbrtowc and mbrtoc32 do not share state (bug 23793).
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <locale.h>
+#include <uchar.h>
+#include <wchar.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  TEST_VERIFY_EXIT (setlocale (LC_ALL, "de_DE.UTF-8") != NULL);
+  const char buf[] = "\u00ff";
+  wchar_t wc = 0;
+  char32_t c32 = 0;
+  size_t ret = mbrtowc (&wc, buf, 1, NULL);
+  TEST_COMPARE (ret, (size_t) -2);
+  ret = mbrtoc32 (&c32, buf, 1, NULL);
+  TEST_COMPARE (ret, (size_t) -2);
+  ret = mbrtowc (&wc, buf + 1, 1, NULL);
+  TEST_COMPARE (ret, 1);
+  TEST_COMPARE (wc, 0xff);
+  ret = mbrtoc32 (&c32, buf + 1, 1, NULL);
+  TEST_COMPARE (ret, 1);
+  TEST_COMPARE (c32, 0xff);
+  return 0;
+}
+
+#include <support/test-driver.c>