newlib-cygwin/newlib/libc/locale
Koichi Murase 973f766f6e Fix duplocale (libc/locale/duplocale.c) which fails to properly call __loadlocale
Problem:

  After  passing  locales  created  by  'duplocale'   to   'uselocale',
  referencing   'MB_CUR_MAX',   which   is   actually   expanded    to
  '__locale_mb_cur_max()' by preprocessors, causes segmentation faults.
  Direct use of locales from 'newlocale' does not  cause  the  problem.
  This is the problem of 'duplocale'.

  $ echo $LANG
  ja_JP.UTF-8
  $ cat test.c
  #include <stdlib.h>
  #include <locale.h>

  volatile int var;

  int main(void) {
    locale_t const loc = newlocale(LC_ALL_MASK, "", NULL);
    locale_t const dup = duplocale(loc);
    locale_t const old = uselocale(dup);
    var = MB_CUR_MAX; /* <-- crashes here */
    uselocale(old);
    freelocale(dup);
    freelocale(loc);
    return 0;
  }
  $ gcc test.c
  $ ./a
  Segmentation fault (core dumped)

  # Note: "core dumped" in the above message was  actually written  in
  # Japanese, but I translated the part to post a mail in English.

Bug:

  In the beginning of '__loadlocale' (newlib/libc/locale/locale.c:501),
  there is a code which checks if the operations can be skipped:

  > /* Avoid doing everything twice if nothing has changed. */
  > if (!strcmp (new_locale, loc->categories[category]))
  >   return loc->categories[category];

  While,   in   the   function   '_duplocale_r'    (newlib/libc/locale/
  duplocale.c), '__loadlocale'  is  called  as  in  the  quoted  codes:

  > /* If the object is not a "C" locale category, copy it.  Just call
  >    __loadlocale.  It knows what to do to replicate the category. */
  > tmp_locale.lc_cat[i].ptr = NULL;
  > tmp_locale.lc_cat[i].buf = NULL;
  > if (!__loadlocale (&tmp_locale, i, tmp_locale.categories[i]))
  >   goto error;

  This call of '__loadlocale' results in the skip check being

    !strcmp(tmp_locale.categories[i], tmp_locale.categories[i]),

  which is always true. This  means  that  the  actual  operations  of
  '__loadLocale' will never be performed for 'duplocale'.

Fix:

  The call of '__loadlocale' in '_duplocale_r' is modified.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2017-03-13 11:12:01 +01:00
..
Makefile.am Add documentation for duplocale, freelocale, newlocale, and uselocale. 2016-08-15 17:34:40 +02:00
Makefile.in Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
duplocale.c Fix duplocale (libc/locale/duplocale.c) which fails to properly call __loadlocale 2017-03-13 11:12:01 +01:00
freelocale.c Add __get_C_locale inline function and fix new locale code for !_MB_CAPABLE targets 2016-08-23 17:57:06 +02:00
lctype.c Remove non-working __part_load_locale function and any related code 2016-08-15 10:56:57 +02:00
lmessages.c Remove non-working __part_load_locale function and any related code 2016-08-15 10:56:57 +02:00
lmonetary.c Remove non-working __part_load_locale function and any related code 2016-08-15 10:56:57 +02:00
lnumeric.c Remove non-working __part_load_locale function and any related code 2016-08-15 10:56:57 +02:00
locale.c Fix duplocale (libc/locale/duplocale.c) which fails to properly call __loadlocale 2017-03-13 11:12:01 +01:00
locale.tex Fix mismatched parentheses in documentation. 2015-06-24 12:23:25 +01:00
localeconv.c Avoid crash when calling __localeconv_l with __C_locale 2016-08-24 19:46:55 +02:00
newlocale.c Fix check for empty locale string in newlocale 2016-10-22 20:22:20 +02:00
nl_langinfo.3 * libc/include/langinfo.h: New file. 2002-08-23 01:56:05 +00:00
nl_langinfo.c nl_langinfo: Add NL_LOCALE_NAME macro 2017-01-20 10:30:47 +01:00
setlocale.h Enforce no arguments for __get_current_locale/__get_C_locale 2016-11-28 10:18:49 +01:00
timelocal.c Remove non-working __part_load_locale function and any related code 2016-08-15 10:56:57 +02:00
uselocale.c Add documentation for duplocale, freelocale, newlocale, and uselocale. 2016-08-15 17:34:40 +02:00