Re: mbrtowc, mbrlen: Work around a NetBSD bug in UTF-8 locales

Bruno Haible via Gnulib discussion list <[email protected]>
Newsgroups gmane.comp.lib.gnulib.bugs
Organization GNU
Message-ID <3682248.V25eIC5XRa@nimes>
> 	mbrtowc, mbrlen: Work around a NetBSD bug in UTF-8 locales.

After this patch, on NetBSD, a testdir of
  mbrtowc mbsrtowcs mbsnrtowcs mbsrtoc32s mbsnrtoc32s
produces these test failures:


FAIL: test-mbsnrtoc32s-3.sh
===========================

../../gltests/test-mbsnrtoc32s.c:234: assertion 'ret == 4' failed
[1]   Abort trap (core dumped) LC_ALL="${testlocale}" ${CHECKER} ./test-mbsnr...
FAIL test-mbsnrtoc32s-3.sh (exit status: 134)

FAIL: test-mbsnrtowcs-3.sh
==========================

../../gltests/test-mbsnrtowcs.c:231: assertion 'ret == 4' failed
[1]   Abort trap (core dumped) LC_ALL="${testlocale}" ${CHECKER} ./test-mbsnr...
FAIL test-mbsnrtowcs-3.sh (exit status: 134)

FAIL: test-mbsrtoc32s-3.sh
==========================

../../gltests/test-mbsrtoc32s.c:234: assertion 'ret == 4' failed
[1]   Abort trap (core dumped) LC_ALL="${testlocale}" ${CHECKER} ./test-mbsrt...
FAIL test-mbsrtoc32s-3.sh (exit status: 134)

FAIL: test-mbsrtowcs-3.sh
=========================

../../gltests/test-mbsrtowcs.c:231: assertion 'ret == 4' failed
[1]   Abort trap (core dumped) LC_ALL="${testlocale}" ${CHECKER} ./test-mbsrt...
FAIL test-mbsrtowcs-3.sh (exit status: 134)


The cause is that the mbstate_t handling in rpl_mbrtowc is not exactly
compatible with the one in mbsrtowcs (due to the 'void *header' pointer).
So, we need to override mbsrtowcs as well. Another reason to do so is,
of course, the handling of invalid byte sequences.


2026-06-02  Bruno Haible  <[email protected]>

	mbrtowc: Work around a NetBSD bug in UTF-8 locales, part 2.
	* m4/mbsrtowcs.m4 (gl_FUNC_MBSRTOWCS): Also set REPLACE_MBSRTOWCS
	to 1 if gl_MBRTOWC_INVALID_UTF8 reports "no".
	* m4/mbsnrtowcs.m4 (gl_FUNC_MBSNRTOWCS): Also set REPLACE_MBSNRTOWCS
	to 1 if gl_MBRTOWC_INVALID_UTF8 reports "no".

diff --git a/m4/mbsnrtowcs.m4 b/m4/mbsnrtowcs.m4
index 500964f8e4..089f58d6df 100644
--- a/m4/mbsnrtowcs.m4
+++ b/m4/mbsnrtowcs.m4
@@ -1,5 +1,5 @@
 # mbsnrtowcs.m4
-# serial 11
+# serial 12
 dnl Copyright (C) 2008, 2010-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -38,6 +38,13 @@ AC_DEFUN([gl_FUNC_MBSNRTOWCS]
           *) REPLACE_MBSNRTOWCS=1 ;;
         esac
       fi
+      if test $REPLACE_MBSNRTOWCS = 0; then
+        gl_MBRTOWC_INVALID_UTF8
+        case "$gl_cv_func_mbrtowc_invalid_UTF8" in
+          *yes) ;;
+          *) REPLACE_MBSNRTOWCS=1 ;;
+        esac
+      fi
     fi
   fi
 ])
diff --git a/m4/mbsrtowcs.m4 b/m4/mbsrtowcs.m4
index df48432b2b..233fdcbc3a 100644
--- a/m4/mbsrtowcs.m4
+++ b/m4/mbsrtowcs.m4
@@ -1,5 +1,5 @@
 # mbsrtowcs.m4
-# serial 19
+# serial 20
 dnl Copyright (C) 2008-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -41,6 +41,13 @@ AC_DEFUN([gl_FUNC_MBSRTOWCS]
           *) REPLACE_MBSRTOWCS=1 ;;
         esac
       fi
+      if test $REPLACE_MBSRTOWCS = 0; then
+        gl_MBRTOWC_INVALID_UTF8
+        case "$gl_cv_func_mbrtowc_invalid_UTF8" in
+          *yes) ;;
+          *) REPLACE_MBSRTOWCS=1 ;;
+        esac
+      fi
     fi
   fi
 ])
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.