From winard@u3717a.dorm.ccu.edu.tw Fri Apr  2 07:22:17 1999
Return-Path: <winard@u3717a.dorm.ccu.edu.tw>
Received: from u3717a.dorm.ccu.edu.tw (u3717a.dorm.ccu.edu.tw [140.123.220.34])
	by hub.freebsd.org (Postfix) with ESMTP id 3C2D614CB3
	for <FreeBSD-gnats-submit@freebsd.org>; Fri,  2 Apr 1999 07:22:11 -0800 (PST)
	(envelope-from winard@u3717a.dorm.ccu.edu.tw)
Received: (from winard@localhost)
	by u3717a.dorm.ccu.edu.tw (8.9.2/8.9.2) id XAA01537;
	Fri, 2 Apr 1999 23:21:25 +0800 (CST)
	(envelope-from winard)
Message-Id: <199904021521.XAA01537@u3717a.dorm.ccu.edu.tw>
Date: Fri, 2 Apr 1999 23:21:25 +0800 (CST)
From: Yung-Jen Hung <winard@u3717a.dorm.ccu.edu.tw>
Reply-To: winard@u3717a.dorm.ccu.edu.tw
To: FreeBSD-gnats-submit@freebsd.org
Subject: Big5 rune function in libc
X-Send-Pr-Version: 3.2

>Number:         10918
>Category:       misc
>Synopsis:       _BIG5_sgetrune() in libc doesn't work well
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    foxfair
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Apr  2 07:30:01 PST 1999
>Closed-Date:    Fri May 7 06:31:32 PDT 1999
>Last-Modified:  Fri May  7 06:32:10 PDT 1999
>Originator:     Yung-Jen Hung
>Release:        FreeBSD 3.1-STABLE i386
>Organization:
Department of Computer Science and Information Engineering,
		 National Chung Cheng University, Taiwan
>Environment:

	None.
	
>Description:

	the function _BIG5_sgetrune() in /usr/src/lib/libc/locale/big5.c
	is wrong,
	as a result it can't convert Big5 multi-byte words into
	wide-character correctly.
	
>How-To-Repeat:

	the following program should output 72 75 6e 65 b4fa b8d5,
	but a segmentation fault occurs now.
	
	before run this program, set the environment variable LD_PRELOAD
	to /usr/lib/libxpg4.so.2 first.
	
/* a program convert characters into rune */

#include <rune.h>
#include <stdio.h>
int main(void)
{
	setrunelocale("zh_TW.BIG5");
	
	const char* src = "rune\xb4\xfa\xb8\xd5";
	const char* ld = src;
	
	rune_t rune[256];
	int num = 0;
	while( ( rune[num] = sgetrune( ld, 3, &ld ) ) != 0 )
		num ++;
	
	for( int c = 0; c < num; c ++ )
		printf( " %02x", rune[c] );
	putchar('\n');
	
}

>Fix:
	change these five lines in big5.c line #85:
	while (len-- >= 0)
		rune = (rune << 8) | ((u_int)(*string++) & 0xff);
	if (result)
		*result = string + len;
	return rune;
	
	to these correct codes:
	while (--len >= 0)
		rune = (rune << 8) | ((u_int)(*string++) & 0xff);
	if (result)
		*result = string;
	return rune;

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->foxfair 
Responsible-Changed-By: foxfair 
Responsible-Changed-When: Sun May 2 04:25:18 PDT 1999 
Responsible-Changed-Why:  
I'll look at it. 
 
State-Changed-From-To: open->closed 
State-Changed-By: foxfair 
State-Changed-When: Fri May 7 06:31:32 PDT 1999 
State-Changed-Why:  
Committed, thanks!(both -current and -stable) 
>Unformatted:
