Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 28 Mar 2014 22:21:18 GMT
From:      Tomohisa Tanaka <tomohisa.tanaka@gmail.com>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   standards/188036: mblen(3) in EUC locales causes crash and segmentation fault.
Message-ID:  <201403282221.s2SMLIHv030818@cgiserv.freebsd.org>
Resent-Message-ID: <201403282230.s2SMU0dM029802@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         188036
>Category:       standards
>Synopsis:       mblen(3) in EUC locales causes crash and segmentation fault.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 28 22:30:00 UTC 2014
>Closed-Date:
>Last-Modified:
>Originator:     Tomohisa Tanaka
>Release:        FreeBSD 9.2-RELEASE-p3
>Organization:
>Environment:
FreeBSD freebsd9 9.2-RELEASE-p3 FreeBSD 9.2-RELEASE-p3 #0: Sat Jan 11 03:25:02 UTC 2014     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64
>Description:
Calling function mblen(3) in EUC locales results in crash and
segmentation fault.  To repeat the problem, both setlocale(3) and
uselocale(3) must be called before calling mblen(3).

I think it is because the value of _CurrentRuneLocale->__variable in
/usr/src/lib/libc/locale/euc.c is always NULL after calling both
setlocale(3) and uselocale(3).

>How-To-Repeat:
% cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <xlocale.h>

int
main(void)
{
  printf("setlocale: %s\n", setlocale(LC_ALL, ""));

  locale_t newLocale = newlocale(LC_ALL_MASK, "C", NULL);
  locale_t oldLocale = uselocale(newLocale);
  /* ... */
  uselocale(oldLocale);

  printf("%d\n", mblen("a", 1));
  return 0;
}
% gcc -g3 -std=c99 main.c
% setenv LC_MESSAGES C
% env LANG=zh_CN.eucCN ./a.out
setlocale: zh_CN.eucCN/zh_CN.eucCN/zh_CN.eucCN/zh_CN.eucCN/zh_CN.eucCN/C
Segmentation fault (core dumped)
% env LANG=ko_KR.eucKR ./a.out
setlocale: ko_KR.eucKR/ko_KR.eucKR/ko_KR.eucKR/ko_KR.eucKR/ko_KR.eucKR/C
Segmentation fault (core dumped)
% env LANG=ja_JP.eucJP ./a.out
setlocale: ja_JP.eucJP/ja_JP.eucJP/ja_JP.eucJP/ja_JP.eucJP/ja_JP.eucJP/C
Segmentation fault (core dumped)
% env LANG=ja_JP.UTF-8 ./a.out
setlocale: ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C
1

>Fix:
diff -ur /usr/src/lib/libc/locale/euc.c locale/euc.c
--- /usr/src/lib/libc/locale/euc.c	2013-09-27 10:07:50.000000000 +0900
+++ locale/euc.c	2014-03-29 06:16:10.000000000 +0900
@@ -134,7 +134,8 @@
 	return (ps == NULL || ((const _EucState *)ps)->want == 0);
 }
 
-#define	CEI	((_EucInfo *)(_CurrentRuneLocale->__variable))
+#define	CEI	\
+	((_EucInfo *)(XLOCALE_CTYPE(__get_locale())->runes->__variable))
 
 #define	_SS2	0x008e
 #define	_SS3	0x008f



Patch attached with submission follows:

diff -ur /usr/src/lib/libc/locale/euc.c locale/euc.c
--- /usr/src/lib/libc/locale/euc.c	2013-09-27 10:07:50.000000000 +0900
+++ locale/euc.c	2014-03-29 06:16:10.000000000 +0900
@@ -134,7 +134,8 @@
 	return (ps == NULL || ((const _EucState *)ps)->want == 0);
 }
 
-#define	CEI	((_EucInfo *)(_CurrentRuneLocale->__variable))
+#define	CEI	\
+	((_EucInfo *)(XLOCALE_CTYPE(__get_locale())->runes->__variable))
 
 #define	_SS2	0x008e
 #define	_SS3	0x008f


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201403282221.s2SMLIHv030818>