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