The new u*-vasnprintf tests that I added today succeed on all
platforms. But on Solaris 11.4, in a testdir that contains also
the 'mcel-prefer' module, they fail:


FAIL: unistdio/test-ulc-vasnprintf3.sh
======================================

../../gltests/unistdio/test-ulc-vasnprintf3.c:226: assertion 'strcmp (result, " 
      h\303\251t\303\251rog\303\251n\303\251it\303\251 33") == 0' failed
Stack trace:
0x40c59d print_stack_trace
        ../../gllib/abort-debug.c:40
0x40c59d rpl_abort
        ../../gllib/abort-debug.c:94
0x40c0f8 test_function
        ../../gltests/unistdio/test-ulc-vasnprintf3.c:226
0x40c487 test_vasnprintf
        ../../gltests/unistdio/test-ulc-vasnprintf3.c:266
0x40c4be main
        ../../gltests/unistdio/test-ulc-vasnprintf3.c:276
../../gltests/unistdio/test-ulc-vasnprintf3.sh: line 20: 6427: Abort(coredump)
../../build-aux/test-driver: line 113: 6425: Abort(coredump)
FAIL unistdio/test-ulc-vasnprintf3.sh (exit status: 262)


The cause, it turns out, is that
  mbsnlen ("h\251", 1)
returns 2. Which is obvious nonsense: mbsnlen (s, count) should always return
a value ≤ count.

So, I wrote unit tests for mbsnlen, and the mcel-based implementation fails
these tests in many places:

../../gltests/test-mbsnlen.c:35: assertion 'mbsnlen ("", 1) == 1' failed
../../gltests/test-mbsnlen.c:36: assertion 'mbsnlen ("\0", 2) == 2' failed
../../gltests/test-mbsnlen.c:38: assertion 'mbsnlen ("H", 0) == 0' failed
../../gltests/test-mbsnlen.c:40: assertion 'mbsnlen ("H", 2) == 2' failed
../../gltests/test-mbsnlen.c:42: assertion 'mbsnlen ("Hello", 0) == 0' failed
../../gltests/test-mbsnlen.c:43: assertion 'mbsnlen ("Hello", 1) == 1' failed
../../gltests/test-mbsnlen.c:44: assertion 'mbsnlen ("Hello", 2) == 2' failed
../../gltests/test-mbsnlen.c:46: assertion 'mbsnlen ("Hello", 6) == 6' failed
../../gltests/test-mbsnlen.c:50: assertion 'mbsnlen ("\303\244\303\266", 0) == 
0' failed
../../gltests/test-mbsnlen.c:51: assertion 'mbsnlen ("\303\244\303\266", 1) == 
1' failed
../../gltests/test-mbsnlen.c:52: assertion 'mbsnlen ("\303\244\303\266", 2) == 
1' failed
../../gltests/test-mbsnlen.c:53: assertion 'mbsnlen ("\303\244\303\266", 3) == 
2' failed
../../gltests/test-mbsnlen.c:55: assertion 'mbsnlen ("\303\244\303\266", 5) == 
3' failed
../../gltests/test-mbsnlen.c:57: assertion 'mbsnlen ("7\342\202\254", 0) == 0' 
failed
../../gltests/test-mbsnlen.c:58: assertion 'mbsnlen ("7\342\202\254", 1) == 1' 
failed
../../gltests/test-mbsnlen.c:59: assertion 'mbsnlen ("7\342\202\254", 2) == 2' 
failed
../../gltests/test-mbsnlen.c:60: assertion 'mbsnlen ("7\342\202\254", 3) == 2' 
failed
../../gltests/test-mbsnlen.c:62: assertion 'mbsnlen ("7\342\202\254", 5) == 3' 
failed
../../gltests/test-mbsnlen.c:64: assertion 'mbsnlen ("\360\237\220\203", 0) == 
0' failed
../../gltests/test-mbsnlen.c:65: assertion 'mbsnlen ("\360\237\220\203", 1) == 
1' failed
../../gltests/test-mbsnlen.c:66: assertion 'mbsnlen ("\360\237\220\203", 2) == 
1' failed
../../gltests/test-mbsnlen.c:67: assertion 'mbsnlen ("\360\237\220\203", 3) == 
1' failed
../../gltests/test-mbsnlen.c:69: assertion 'mbsnlen ("\360\237\220\203", 5) == 
2' failed
../../gltests/test-mbsnlen.c:73: assertion 'mbsnlen ("\342\202", 2) == 1' failed
../../gltests/test-mbsnlen.c:74: assertion 'mbsnlen ("\360\237\220", 3) == 1' 
failed

The two attached patches make the tests pass. At this point, I don't have
time to deal with the MEE vs. SEE behaviour differences; that has to wait
for later.


2024-06-18  Bruno Haible  <br...@clisp.org>

        mbsnlen: Add tests.
        * tests/test-mbsnlen.c: New file.
        * tests/test-mbsnlen.sh: New file, based on tests/test-mbsspn.sh.
        * modules/mbsnlen-tests: New file.

        mbsnlen: Fix bug (regression 2023-09-26).
        * lib/mbsnlen.c (mbsnlen): Fix bug in GNULIB_MCEL_PREFER implementation.

>From b3d39349c9ac177b4640cd0e7d8a9a1e21be58e9 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Wed, 19 Jun 2024 02:31:30 +0200
Subject: [PATCH 1/2] mbsnlen: Fix bug (regression 2023-09-26).

* lib/mbsnlen.c (mbsnlen): Fix bug in GNULIB_MCEL_PREFER implementation.
---
 ChangeLog     | 5 +++++
 lib/mbsnlen.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 7e66ba5b11..fbde74072f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-06-18  Bruno Haible  <br...@clisp.org>
+
+	mbsnlen: Fix bug (regression 2023-09-26).
+	* lib/mbsnlen.c (mbsnlen): Fix bug in GNULIB_MCEL_PREFER implementation.
+
 2024-06-18  Bruno Haible  <br...@clisp.org>
 
 	mbslen: Add tests.
diff --git a/lib/mbsnlen.c b/lib/mbsnlen.c
index baadf163ca..1ce79bfb4b 100644
--- a/lib/mbsnlen.c
+++ b/lib/mbsnlen.c
@@ -40,7 +40,7 @@ mbsnlen (const char *string, size_t len)
       const char *string_end = string + len;
 
 #if GNULIB_MCEL_PREFER
-      for (; *string; string += mcel_scan (string, string_end).len)
+      for (; string < string_end; string += mcel_scan (string, string_end).len)
         count++;
 #else
       mbif_state_t state;
-- 
2.34.1

From cc0b231c72caaf70656ff84b481ee9b0a93597e8 Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Wed, 19 Jun 2024 02:32:00 +0200
Subject: [PATCH 2/2] mbsnlen: Add tests.

* tests/test-mbsnlen.c: New file.
* tests/test-mbsnlen.sh: New file, based on tests/test-mbsspn.sh.
* modules/mbsnlen-tests: New file.
---
 ChangeLog             |  5 +++
 modules/mbsnlen-tests | 18 +++++++++
 tests/test-mbsnlen.c  | 90 +++++++++++++++++++++++++++++++++++++++++++
 tests/test-mbsnlen.sh | 15 ++++++++
 4 files changed, 128 insertions(+)
 create mode 100644 modules/mbsnlen-tests
 create mode 100644 tests/test-mbsnlen.c
 create mode 100755 tests/test-mbsnlen.sh

diff --git a/ChangeLog b/ChangeLog
index fbde74072f..330bf49b32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-06-18  Bruno Haible  <br...@clisp.org>
 
+	mbsnlen: Add tests.
+	* tests/test-mbsnlen.c: New file.
+	* tests/test-mbsnlen.sh: New file, based on tests/test-mbsspn.sh.
+	* modules/mbsnlen-tests: New file.
+
 	mbsnlen: Fix bug (regression 2023-09-26).
 	* lib/mbsnlen.c (mbsnlen): Fix bug in GNULIB_MCEL_PREFER implementation.
 
diff --git a/modules/mbsnlen-tests b/modules/mbsnlen-tests
new file mode 100644
index 0000000000..d6fad3c8cb
--- /dev/null
+++ b/modules/mbsnlen-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-mbsnlen.sh
+tests/test-mbsnlen.c
+tests/macros.h
+m4/locale-fr.m4
+m4/codeset.m4
+
+Depends-on:
+setlocale
+
+configure.ac:
+gt_LOCALE_FR_UTF8
+
+Makefile.am:
+TESTS += test-mbsnlen.sh
+TESTS_ENVIRONMENT += LOCALE_FR_UTF8='@LOCALE_FR_UTF8@'
+check_PROGRAMS += test-mbsnlen
+test_mbsnlen_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) $(LIBC32CONV)
diff --git a/tests/test-mbsnlen.c b/tests/test-mbsnlen.c
new file mode 100644
index 0000000000..a424ccd57f
--- /dev/null
+++ b/tests/test-mbsnlen.c
@@ -0,0 +1,90 @@
+/* Test of searching a string for a character outside a given set of characters.
+   Copyright (C) 2007-2024 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <br...@clisp.org>, 2007.  */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <locale.h>
+
+#include "macros.h"
+
+/* The mcel-based implementation of mbsnlen behaves differently than the
+   original one.  Namely, for invalid/incomplete byte sequences:
+   Where we ideally should have multi-byte-per-encoding-error (MEE) behaviour
+   everywhere, mcel implements single-byte-per-encoding-error (SEE) behaviour.
+   See <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00131.html>,
+       <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00145.html>.
+   Therefore, here we have different expected results, depending on the
+   implementation.  */
+#if GNULIB_MCEL_PREFER
+# define OR(a,b) b
+#else
+# define OR(a,b) a
+#endif
+
+int
+main ()
+{
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  ASSERT (mbsnlen ("", 0) == 0);
+  ASSERT (mbsnlen ("", 1) == 1);
+  ASSERT (mbsnlen ("\0", 2) == 2);
+
+  ASSERT (mbsnlen ("H", 0) == 0);
+  ASSERT (mbsnlen ("H", 1) == 1);
+  ASSERT (mbsnlen ("H", 2) == 2);
+
+  ASSERT (mbsnlen ("Hello", 0) == 0);
+  ASSERT (mbsnlen ("Hello", 1) == 1);
+  ASSERT (mbsnlen ("Hello", 2) == 2);
+  ASSERT (mbsnlen ("Hello", 5) == 5);
+  ASSERT (mbsnlen ("Hello", 6) == 6);
+
+  /* The following tests shows how mbsnlen() is different from strnlen().  */
+  /* "äö" */
+  ASSERT (mbsnlen ("\303\244\303\266", 0) == 0);
+  ASSERT (mbsnlen ("\303\244\303\266", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\303\244\303\266", 2) == 1);
+  ASSERT (mbsnlen ("\303\244\303\266", 3) == 2); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\303\244\303\266", 4) == 2);
+  ASSERT (mbsnlen ("\303\244\303\266", 5) == 3);
+  /* "7€" */
+  ASSERT (mbsnlen ("7\342\202\254", 0) == 0);
+  ASSERT (mbsnlen ("7\342\202\254", 1) == 1);
+  ASSERT (mbsnlen ("7\342\202\254", 2) == 2); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("7\342\202\254", 3) == OR(2,3)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("7\342\202\254", 4) == 2);
+  ASSERT (mbsnlen ("7\342\202\254", 5) == 3);
+  /* "🐃" */
+  ASSERT (mbsnlen ("\360\237\220\203", 0) == 0);
+  ASSERT (mbsnlen ("\360\237\220\203", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 2) == OR(1,2)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 3) == OR(1,3)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 4) == 1);
+  ASSERT (mbsnlen ("\360\237\220\203", 5) == 2);
+
+  ASSERT (mbsnlen ("\303", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\342\202", 2) == OR(1,2)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220", 3) == OR(1,3)); /* invalid multibyte sequence */
+
+  return test_exit_status;
+}
diff --git a/tests/test-mbsnlen.sh b/tests/test-mbsnlen.sh
new file mode 100755
index 0000000000..48fa3f2801
--- /dev/null
+++ b/tests/test-mbsnlen.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific UTF-8 locale is installed.
+: "${LOCALE_FR_UTF8=fr_FR.UTF-8}"
+if test $LOCALE_FR_UTF8 = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no french Unicode locale is installed"
+  else
+    echo "Skipping test: no french Unicode locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_FR_UTF8 \
+${CHECKER} ./test-mbsnlen${EXEEXT}
-- 
2.34.1

Reply via email to