Subject: lib/20577: btowc() can't convert null character
To: None <gnats-bugs@gnats.netbsd.org>
From: None <sigsegv@s25.xrea.com>
List: netbsd-bugs
Date: 03/04/2003 03:17:21
>Number:         20577
>Category:       lib
>Synopsis:       btowc() can't convert null character
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 04 10:22:00 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     tnozaki
>Release:        NetBSD-1.6P
>Organization:
>Environment:
NetBSD fly 1.6P NetBSD 1.6P (FLY) #0: Tue Mar 4 10:53:35 UTC 2003 tnozaki@fly:/usr/src/sys/arch/i386/compile/FLY i386
libc.so.12.96 lib{BIG5,EUC,EUCTW,ISO2022,MSKanji,UTF8}.so.4.0

>Description:
btowc() can't convert null character

When translating a null character ('\0' is valid singlebyte character) by btowc,
mbrtowc (used in btowc's internal) may returns 0 (it's correct).
but btowc checks only if it returns 1 or not.
such case, we have bad WEOF.
should be check whether mbrtowc returns 0 or not, too.

#wctob is ok, cause wcrtomb returns 1 when translate null character.

>How-To-Repeat:
compile and run below.
--
#include <locale.h>
#include <wchar.h>
main(void) {
	setlocale(LC_CTYPE, "ja_JP.eucJP");
	if (WEOF == btowc((int)(unsigned char)'\0'))
		printf("btowc fail.\n");
	else
		printf("btowc success.\n");
}
--
NetBSD will print "btowc fail.\n"
but glibc does "btowc success.\n"

>Fix:
Index: btowc.c
===================================================================
RCS file: /home/cvs/NetBSD/src/lib/libc/locale/btowc.c,v
retrieving revision 1.2
diff -u -r1.2 btowc.c
--- btowc.c	3 Mar 2003 07:39:54 -0000	1.2
+++ btowc.c	4 Mar 2003 10:19:55 -0000
@@ -53,8 +53,12 @@
 
 	mb = (unsigned char)c;
 	memset(&mbs, 0, sizeof(mbs));
-	if (mbrtowc(&wc, &mb, 1, &mbs) != 1)
+	switch (mbrtowc(&wc, &mb, 1, &mbs) {
+	case (size_t)0:
+	case (size_t)1:
+		break;
+	default:
 		return (WEOF);
-
+	}
 	return ((wint_t)wc);
 }
Index: wctob.c
===================================================================
RCS file: /home/cvs/NetBSD/src/lib/libc/locale/wctob.c,v
retrieving revision 1.2
diff -u -r1.2 wctob.c
--- wctob.c	3 Mar 2003 08:25:00 -0000	1.2
+++ wctob.c	4 Mar 2003 10:20:12 -0000
@@ -53,7 +53,7 @@
 
 	memset(&mbs, 0, sizeof(mbs));
 	if (wcrtomb(mb, wc, &mbs) != 1)
-		return (WEOF);
+		return (EOF);
 
 	return ((int)(unsigned char)mb[0]);
 }

and SUSv3 said, btowc/wctob defined no errno value.
but mbrtowc/wcrtomb sometimes set EILSEQ.
so prefer to keep errno value?

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