446cf2
commit 7d4ec75e111291851620c6aa2c4460647b7fd50d
446cf2
Author: Arjun Shankar <arjun@redhat.com>
446cf2
Date:   Fri Sep 25 14:47:06 2020 +0200
446cf2
446cf2
    intl: Handle translation output codesets with suffixes [BZ #26383]
446cf2
    
446cf2
    Commit 91927b7c7643 (Rewrite iconv option parsing [BZ #19519]) did not
446cf2
    handle cases where the output codeset for translations (via the `gettext'
446cf2
    family of functions) might have a caller specified encoding suffix such as
446cf2
    TRANSLIT or IGNORE.  This led to a regression where translations did not
446cf2
    work when the codeset had a suffix.
446cf2
    
446cf2
    This commit fixes the above issue by parsing any suffixes passed to
446cf2
    __dcigettext and adds two new test-cases to intl/tst-codeset.c to
446cf2
    verify correct behaviour.  The iconv-internal function __gconv_create_spec
446cf2
    and the static iconv-internal function gconv_destroy_spec are now visible
446cf2
    internally within glibc and used in intl/dcigettext.c.
446cf2
446cf2
diff --git a/iconv/Versions b/iconv/Versions
446cf2
index 8a5f4cf780b18925..d51af52fa34b8793 100644
446cf2
--- a/iconv/Versions
446cf2
+++ b/iconv/Versions
446cf2
@@ -6,7 +6,9 @@ libc {
446cf2
   GLIBC_PRIVATE {
446cf2
     # functions shared with iconv program
446cf2
     __gconv_get_alias_db; __gconv_get_cache; __gconv_get_modules_db;
446cf2
-    __gconv_open; __gconv_create_spec;
446cf2
+
446cf2
+    # functions used elsewhere in glibc
446cf2
+    __gconv_open; __gconv_create_spec; __gconv_destroy_spec;
446cf2
 
446cf2
     # function used by the gconv modules
446cf2
     __gconv_transliterate;
446cf2
diff --git a/iconv/gconv_charset.c b/iconv/gconv_charset.c
446cf2
index 6ccd0773ccb6cd27..4ba0aa99f5dae7f7 100644
446cf2
--- a/iconv/gconv_charset.c
446cf2
+++ b/iconv/gconv_charset.c
446cf2
@@ -216,3 +216,13 @@ out:
446cf2
   return ret;
446cf2
 }
446cf2
 libc_hidden_def (__gconv_create_spec)
446cf2
+
446cf2
+
446cf2
+void
446cf2
+__gconv_destroy_spec (struct gconv_spec *conv_spec)
446cf2
+{
446cf2
+  free (conv_spec->fromcode);
446cf2
+  free (conv_spec->tocode);
446cf2
+  return;
446cf2
+}
446cf2
+libc_hidden_def (__gconv_destroy_spec)
446cf2
diff --git a/iconv/gconv_charset.h b/iconv/gconv_charset.h
446cf2
index b85d80313030b649..4b98073389bd8707 100644
446cf2
--- a/iconv/gconv_charset.h
446cf2
+++ b/iconv/gconv_charset.h
446cf2
@@ -48,33 +48,6 @@
446cf2
 #define GCONV_IGNORE_ERRORS_SUFFIX "IGNORE"
446cf2
 
446cf2
 
446cf2
-/* This function accepts the charset names of the source and destination of the
446cf2
-   conversion and populates *conv_spec with an equivalent conversion
446cf2
-   specification that may later be used by __gconv_open.  The charset names
446cf2
-   might contain options in the form of suffixes that alter the conversion,
446cf2
-   e.g. "ISO-10646/UTF-8/TRANSLIT".  It processes the charset names, ignoring
446cf2
-   and truncating any suffix options in fromcode, and processing and truncating
446cf2
-   any suffix options in tocode.  Supported suffix options ("TRANSLIT" or
446cf2
-   "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec
446cf2
-   to be set to true.  Unrecognized suffix options are silently discarded.  If
446cf2
-   the function succeeds, it returns conv_spec back to the caller.  It returns
446cf2
-   NULL upon failure.  */
446cf2
-struct gconv_spec *
446cf2
-__gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode,
446cf2
-                     const char *tocode);
446cf2
-libc_hidden_proto (__gconv_create_spec)
446cf2
-
446cf2
-
446cf2
-/* This function frees all heap memory allocated by __gconv_create_spec.  */
446cf2
-static void __attribute__ ((unused))
446cf2
-gconv_destroy_spec (struct gconv_spec *conv_spec)
446cf2
-{
446cf2
-  free (conv_spec->fromcode);
446cf2
-  free (conv_spec->tocode);
446cf2
-  return;
446cf2
-}
446cf2
-
446cf2
-
446cf2
 /* This function copies in-order, characters from the source 's' that are
446cf2
    either alpha-numeric or one in one of these: "_-.,:/" - into the destination
446cf2
    'wp' while dropping all other characters.  In the process, it converts all
446cf2
diff --git a/iconv/gconv_int.h b/iconv/gconv_int.h
446cf2
index 4748e9b1fa3b5426..8067a341b0903e1b 100644
446cf2
--- a/iconv/gconv_int.h
446cf2
+++ b/iconv/gconv_int.h
446cf2
@@ -170,6 +170,27 @@ extern int __gconv_open (struct gconv_spec *conv_spec,
446cf2
                          __gconv_t *handle, int flags);
446cf2
 libc_hidden_proto (__gconv_open)
446cf2
 
446cf2
+/* This function accepts the charset names of the source and destination of the
446cf2
+   conversion and populates *conv_spec with an equivalent conversion
446cf2
+   specification that may later be used by __gconv_open.  The charset names
446cf2
+   might contain options in the form of suffixes that alter the conversion,
446cf2
+   e.g. "ISO-10646/UTF-8/TRANSLIT".  It processes the charset names, ignoring
446cf2
+   and truncating any suffix options in fromcode, and processing and truncating
446cf2
+   any suffix options in tocode.  Supported suffix options ("TRANSLIT" or
446cf2
+   "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec
446cf2
+   to be set to true.  Unrecognized suffix options are silently discarded.  If
446cf2
+   the function succeeds, it returns conv_spec back to the caller.  It returns
446cf2
+   NULL upon failure.  */
446cf2
+extern struct gconv_spec *
446cf2
+__gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode,
446cf2
+                     const char *tocode);
446cf2
+libc_hidden_proto (__gconv_create_spec)
446cf2
+
446cf2
+/* This function frees all heap memory allocated by __gconv_create_spec.  */
446cf2
+extern void
446cf2
+__gconv_destroy_spec (struct gconv_spec *conv_spec);
446cf2
+libc_hidden_proto (__gconv_destroy_spec)
446cf2
+
446cf2
 /* Free resources associated with transformation descriptor CD.  */
446cf2
 extern int __gconv_close (__gconv_t cd)
446cf2
      attribute_hidden;
446cf2
diff --git a/iconv/iconv_open.c b/iconv/iconv_open.c
446cf2
index 59d1ef4f07ed1022..46da33bca6c24af0 100644
446cf2
--- a/iconv/iconv_open.c
446cf2
+++ b/iconv/iconv_open.c
446cf2
@@ -39,7 +39,7 @@ iconv_open (const char *tocode, const char *fromcode)
446cf2
 
446cf2
   int res = __gconv_open (&conv_spec, &cd, 0);
446cf2
 
446cf2
-  gconv_destroy_spec (&conv_spec);
446cf2
+  __gconv_destroy_spec (&conv_spec);
446cf2
 
446cf2
   if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
446cf2
     {
446cf2
diff --git a/iconv/iconv_prog.c b/iconv/iconv_prog.c
446cf2
index 552efac81660e82a..e26e9d02ca4121a7 100644
446cf2
--- a/iconv/iconv_prog.c
446cf2
+++ b/iconv/iconv_prog.c
446cf2
@@ -184,7 +184,7 @@ main (int argc, char *argv[])
446cf2
       /* Let's see whether we have these coded character sets.  */
446cf2
       res = __gconv_open (&conv_spec, &cd, 0);
446cf2
 
446cf2
-      gconv_destroy_spec (&conv_spec);
446cf2
+      __gconv_destroy_spec (&conv_spec);
446cf2
 
446cf2
       if (res != __GCONV_OK)
446cf2
 	{
446cf2
diff --git a/intl/dcigettext.c b/intl/dcigettext.c
446cf2
index ed48fc8d3e96c7ba..7ebe67b4ac2113e9 100644
446cf2
--- a/intl/dcigettext.c
446cf2
+++ b/intl/dcigettext.c
446cf2
@@ -1121,15 +1121,18 @@ _nl_find_msg (struct loaded_l10nfile *domain_file,
446cf2
 
446cf2
 # ifdef _LIBC
446cf2
 
446cf2
-		      struct gconv_spec conv_spec
446cf2
-		        = { .fromcode = norm_add_slashes (charset, ""),
446cf2
-		            .tocode = norm_add_slashes (outcharset, ""),
446cf2
-		            /* We always want to use transliteration.  */
446cf2
-		            .translit = true,
446cf2
-		            .ignore = false
446cf2
-		          };
446cf2
+		      struct gconv_spec conv_spec;
446cf2
+
446cf2
+                      __gconv_create_spec (&conv_spec, charset, outcharset);
446cf2
+
446cf2
+		      /* We always want to use transliteration.  */
446cf2
+                      conv_spec.translit = true;
446cf2
+
446cf2
 		      int r = __gconv_open (&conv_spec, &convd->conv,
446cf2
 		                            GCONV_AVOID_NOCONV);
446cf2
+
446cf2
+                      __gconv_destroy_spec (&conv_spec);
446cf2
+
446cf2
 		      if (__builtin_expect (r != __GCONV_OK, 0))
446cf2
 			{
446cf2
 			  /* If the output encoding is the same there is
446cf2
diff --git a/intl/tst-codeset.c b/intl/tst-codeset.c
446cf2
index e71382aeeeca477b..52e4aaa6ffd3afdb 100644
446cf2
--- a/intl/tst-codeset.c
446cf2
+++ b/intl/tst-codeset.c
446cf2
@@ -22,13 +22,11 @@
446cf2
 #include <stdio.h>
446cf2
 #include <stdlib.h>
446cf2
 #include <string.h>
446cf2
+#include <support/check.h>
446cf2
 
446cf2
 static int
446cf2
 do_test (void)
446cf2
 {
446cf2
-  char *s;
446cf2
-  int result = 0;
446cf2
-
446cf2
   unsetenv ("LANGUAGE");
446cf2
   unsetenv ("OUTPUT_CHARSET");
446cf2
   setlocale (LC_ALL, "de_DE.ISO-8859-1");
446cf2
@@ -36,25 +34,21 @@ do_test (void)
446cf2
   bindtextdomain ("codeset", OBJPFX "domaindir");
446cf2
 
446cf2
   /* Here we expect output in ISO-8859-1.  */
446cf2
-  s = gettext ("cheese");
446cf2
-  if (strcmp (s, "K\344se"))
446cf2
-    {
446cf2
-      printf ("call 1 returned: %s\n", s);
446cf2
-      result = 1;
446cf2
-    }
446cf2
+  TEST_COMPARE_STRING (gettext ("cheese"), "K\344se");
446cf2
 
446cf2
+  /* Here we expect output in UTF-8.  */
446cf2
   bind_textdomain_codeset ("codeset", "UTF-8");
446cf2
+  TEST_COMPARE_STRING (gettext ("cheese"), "K\303\244se");
446cf2
 
446cf2
-  /* Here we expect output in UTF-8.  */
446cf2
-  s = gettext ("cheese");
446cf2
-  if (strcmp (s, "K\303\244se"))
446cf2
-    {
446cf2
-      printf ("call 2 returned: %s\n", s);
446cf2
-      result = 1;
446cf2
-    }
446cf2
-
446cf2
-  return result;
446cf2
+  /* `a with umlaut' is transliterated to `ae'.  */
446cf2
+  bind_textdomain_codeset ("codeset", "ASCII//TRANSLIT");
446cf2
+  TEST_COMPARE_STRING (gettext ("cheese"), "Kaese");
446cf2
+
446cf2
+  /* Transliteration also works by default even if not set.  */
446cf2
+  bind_textdomain_codeset ("codeset", "ASCII");
446cf2
+  TEST_COMPARE_STRING (gettext ("cheese"), "Kaese");
446cf2
+
446cf2
+  return 0;
446cf2
 }
446cf2
 
446cf2
-#define TEST_FUNCTION do_test ()
446cf2
-#include "../test-skeleton.c"
446cf2
+#include <support/test-driver.c>