Module Name:    src
Committed By:   riastradh
Date:           Tue Aug 20 17:43:41 UTC 2024

Modified Files:
        src/lib/libc/locale: c32rtomb.c

Log Message:
c32rtomb(3): Fix type of wcrtomb_l return value.

This was from `int wctomb_l(...)' in an earlier draft and I didn't
update it to size_t when I changed the draft to wcrtomb_l.  Caught by
lint.

`wc_len' mirrors `mb_len' in the complementary code in mbrtoc32(3) to
avoid clash with standard C function mblen(3).

PR lib/58612: c8rtomb/c16rtomb/c32rtomb yield suboptimal shift
sequences


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/lib/libc/locale/c32rtomb.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/locale/c32rtomb.c
diff -u src/lib/libc/locale/c32rtomb.c:1.4 src/lib/libc/locale/c32rtomb.c:1.5
--- src/lib/libc/locale/c32rtomb.c:1.4	Mon Aug 19 16:22:10 2024
+++ src/lib/libc/locale/c32rtomb.c	Tue Aug 20 17:43:41 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: c32rtomb.c,v 1.4 2024/08/19 16:22:10 riastradh Exp $	*/
+/*	$NetBSD: c32rtomb.c,v 1.5 2024/08/20 17:43:41 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2024 The NetBSD Foundation, Inc.
@@ -49,7 +49,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: c32rtomb.c,v 1.4 2024/08/19 16:22:10 riastradh Exp $");
+__RCSID("$NetBSD: c32rtomb.c,v 1.5 2024/08/20 17:43:41 riastradh Exp $");
 
 #include "namespace.h"
 
@@ -98,7 +98,7 @@ c32rtomb_l(char *restrict s, char32_t c3
 	size_t srcleft, dstleft, inval;
 	mbstate_t mbrtowcstate = {0};
 	wchar_t wc;
-	int wlen;
+	size_t wc_len;
 	size_t len;
 	int error, errno_save;
 
@@ -194,16 +194,17 @@ c32rtomb_l(char *restrict s, char32_t c3
 	 *
 	 * XXX What about combining characters?
 	 */
-	wlen = mbrtowc_l(&wc, buf, len, &mbrtowcstate, loc);
-	switch (wlen) {
-	case -1:		/* error, with errno set */
+	wc_len = mbrtowc_l(&wc, buf, len, &mbrtowcstate, loc);
+	switch (wc_len) {
+	case (size_t)-1:	/* error, with errno set */
+		len = (size_t)-1;
 		goto out;
 	case 0:			/* decoded NUL */
 		wc = 0;		/* paranoia */
+		len = wc_len;
 		break;
 	default:		/* decoded wc */
-		_DIAGASSERT(wlen > 0);
-		_DIAGASSERT((size_t)wlen <= len);
+		_DIAGASSERT(wc_len <= len);
 	}
 
 	/*

Reply via email to