(root)/
glibc-2.38/
localedata/
tst-setlocale3.c
       1  /* Regression test for setlocale invalid environment variable handling.
       2     Copyright (C) 2014-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <locale.h>
      20  #include <stdio.h>
      21  #include <stdlib.h>
      22  #include <string.h>
      23  
      24  /* The result of setlocale may be overwritten by subsequent calls, so
      25     this wrapper makes a copy.  */
      26  static char *
      27  setlocale_copy (int category, const char *locale)
      28  {
      29    const char *result = setlocale (category, locale);
      30    if (result == NULL)
      31      return NULL;
      32    return strdup (result);
      33  }
      34  
      35  static char *de_locale;
      36  
      37  static void
      38  setlocale_fail (const char *envstring)
      39  {
      40    setenv ("LC_CTYPE", envstring, 1);
      41    if (setlocale (LC_CTYPE, "") != NULL)
      42      {
      43        printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
      44        exit (1);
      45      }
      46    const char *newloc = setlocale (LC_CTYPE, NULL);
      47    if (strcmp (newloc, de_locale) != 0)
      48      {
      49        printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
      50  	      envstring, newloc);
      51        exit (1);
      52      }
      53  }
      54  
      55  static void
      56  setlocale_success (const char *envstring)
      57  {
      58    setenv ("LC_CTYPE", envstring, 1);
      59    char *newloc = setlocale_copy (LC_CTYPE, "");
      60    if (newloc == NULL)
      61      {
      62        printf ("setlocale for \"%s\": %m\n", envstring);
      63        exit (1);
      64      }
      65    if (strcmp (newloc, de_locale) == 0)
      66      {
      67        printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
      68  	      envstring, de_locale);
      69        exit (1);
      70      }
      71    if (setlocale (LC_CTYPE, de_locale) == NULL)
      72      {
      73        printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
      74  	      de_locale, envstring);
      75        exit (1);
      76      }
      77    char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
      78    if (newloc2 == NULL)
      79      {
      80        printf ("restoring locale \"%s\" following \"%s\": %m\n",
      81  	      newloc, envstring);
      82        exit (1);
      83      }
      84    if (strcmp (newloc, newloc2) != 0)
      85      {
      86        printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
      87  	      envstring, newloc, newloc2);
      88        exit (1);
      89      }
      90    free (newloc);
      91    free (newloc2);
      92  
      93    if (setlocale (LC_CTYPE, de_locale) == NULL)
      94      {
      95        printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
      96  	      de_locale, envstring);
      97        exit (1);
      98      }
      99  }
     100  
     101  /* Checks that a known-good locale still works if LC_ALL contains a
     102     value which should be ignored.  */
     103  static void
     104  setlocale_ignore (const char *to_ignore)
     105  {
     106    const char *fr_locale = "fr_FR.UTF-8";
     107    setenv ("LC_CTYPE", fr_locale, 1);
     108    char *expected_locale = setlocale_copy (LC_CTYPE, "");
     109    if (expected_locale == NULL)
     110      {
     111        printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
     112        exit (1);
     113      }
     114    if (setlocale (LC_CTYPE, de_locale) == NULL)
     115      {
     116        printf ("failed to restore locale: %m\n");
     117        exit (1);
     118      }
     119    unsetenv ("LC_CTYPE");
     120  
     121    setenv ("LC_ALL", to_ignore, 1);
     122    setenv ("LC_CTYPE", fr_locale, 1);
     123    const char *actual_locale = setlocale (LC_CTYPE, "");
     124    if (actual_locale == NULL)
     125      {
     126        printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
     127  	      fr_locale);
     128        exit (1);
     129      }
     130    if (strcmp (actual_locale, expected_locale) != 0)
     131      {
     132        printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
     133  	      actual_locale, expected_locale);
     134        exit (1);
     135      }
     136    unsetenv ("LC_CTYPE");
     137    setlocale_success (fr_locale);
     138    unsetenv ("LC_ALL");
     139    free (expected_locale);
     140  }
     141  
     142  static int
     143  do_test (void)
     144  {
     145    /* The glibc test harness sets this environment variable
     146       uncondionally.  */
     147    unsetenv ("LC_ALL");
     148  
     149    de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
     150    if (de_locale == NULL)
     151      {
     152        printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
     153        return 1;
     154      }
     155    setlocale_success ("C");
     156    setlocale_success ("en_US.UTF-8");
     157    setlocale_success ("/en_US.UTF-8");
     158    setlocale_success ("//en_US.UTF-8");
     159    setlocale_ignore ("");
     160  
     161    setlocale_fail ("does-not-exist");
     162    setlocale_fail ("/");
     163    setlocale_fail ("/../localedata/en_US.UTF-8");
     164    setlocale_fail ("en_US.UTF-8/");
     165    setlocale_fail ("en_US.UTF-8/..");
     166    setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
     167    setlocale_fail ("../localedata/en_US.UTF-8");
     168    {
     169      size_t large_length = 1024;
     170      char *large_name = malloc (large_length + 1);
     171      if (large_name == NULL)
     172        {
     173  	puts ("malloc failure");
     174  	return 1;
     175        }
     176      memset (large_name, '/', large_length);
     177      const char *suffix = "en_US.UTF-8";
     178      strcpy (large_name + large_length - strlen (suffix), suffix);
     179      setlocale_fail (large_name);
     180      free (large_name);
     181    }
     182    {
     183      size_t huge_length = 64 * 1024 * 1024;
     184      char *huge_name = malloc (huge_length + 1);
     185      if (huge_name == NULL)
     186        {
     187  	puts ("malloc failure");
     188  	return 1;
     189        }
     190      memset (huge_name, 'X', huge_length);
     191      huge_name[huge_length] = '\0';
     192      /* Construct a composite locale specification. */
     193      const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
     194      memcpy (huge_name, prefix, strlen (prefix));
     195      setlocale_fail (huge_name);
     196      free (huge_name);
     197    }
     198  
     199    return 0;
     200  }
     201  
     202  #define TEST_FUNCTION do_test ()
     203  #include "../test-skeleton.c"