]> Savannah Git Hosting - gnulib.git/commitdiff
stdlib: Work around MB_CUR_MAX bug on Solaris 10.
authorBruno Haible <bruno@clisp.org>
Tue, 4 Apr 2023 11:03:53 +0000 (13:03 +0200)
committerBruno Haible <bruno@clisp.org>
Tue, 4 Apr 2023 11:10:39 +0000 (13:10 +0200)
* lib/stdlib.in.h (gl_MB_CUR_MAX): New function.
(MB_CUR_MAX, GNULIB_defined_MB_CUR_MAX): New macros.
* m4/stdlib_h.m4 (gl_STDLIB_H): Test whether MB_CUR_MAX is correct.
(gl_STDLIB_H_DEFAULTS): Initialize REPLACE_MB_CUR_MAX.
* modules/stdlib (Files): Add m4/locale-fr.m4.
(Makefile.am): Substitute REPLACE_MB_CUR_MAX.
* doc/posix-headers/stdlib.texi: Mention the Solaris 10 bug.

ChangeLog
doc/posix-headers/stdlib.texi
lib/stdlib.in.h
m4/stdlib_h.m4
modules/stdlib

index 9a78e7f77179832c13c1a53056276c7bb774ab33..5f64b7c1c30282af54f4fe978904ea35e268ee44 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2023-04-04  Bruno Haible  <bruno@clisp.org>
+
+       stdlib: Work around MB_CUR_MAX bug on Solaris 10.
+       * lib/stdlib.in.h (gl_MB_CUR_MAX): New function.
+       (MB_CUR_MAX, GNULIB_defined_MB_CUR_MAX): New macros.
+       * m4/stdlib_h.m4 (gl_STDLIB_H): Test whether MB_CUR_MAX is correct.
+       (gl_STDLIB_H_DEFAULTS): Initialize REPLACE_MB_CUR_MAX.
+       * modules/stdlib (Files): Add m4/locale-fr.m4.
+       (Makefile.am): Substitute REPLACE_MB_CUR_MAX.
+       * doc/posix-headers/stdlib.texi: Mention the Solaris 10 bug.
+
 2023-04-04  Bruno Haible  <bruno@clisp.org>
 
        *c32*: Inline most functions on glibc and musl libc.
index 612e794068b6dd8ed0367c49347f972be61059c7..ac7efbda8465a446f52c777f0afaa104e55a5dea 100644 (file)
@@ -14,6 +14,10 @@ some platforms.
 Some platforms provide a @code{NULL} macro that cannot be used in arbitrary
 expressions:
 NetBSD 5.0
+@item
+The value of @code{MB_CUR_MAX} is too small (3 instead of 4) in UTF-8 locales
+on some platforms:
+Solaris 10.
 @end itemize
 
 Portability problems fixed by the Gnulib module @code{system-posix}:
index 4ecfc96a6fd6522dccfcf8eb17092e8b8c09293f..c18a16bacdd7bf78322e53b8a66efdcb9173a345 100644 (file)
@@ -589,6 +589,21 @@ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - "
 # endif
 #endif
 
+/* Return maximum number of bytes of a multibyte character.  */
+#if @REPLACE_MB_CUR_MAX@
+# if !GNULIB_defined_MB_CUR_MAX
+static inline
+int gl_MB_CUR_MAX (void)
+{
+  /* Turn the value 3 to the value 4, as needed for the UTF-8 encoding.  */
+  return MB_CUR_MAX + (MB_CUR_MAX == 3);
+}
+#  undef MB_CUR_MAX
+#  define MB_CUR_MAX gl_MB_CUR_MAX ()
+#  define GNULIB_defined_MB_CUR_MAX 1
+# endif
+#endif
+
 /* Convert a string to a wide string.  */
 #if @GNULIB_MBSTOWCS@
 # if @REPLACE_MBSTOWCS@
index ac28ed9efcc61ac4c5e3b5068dc43bfc3c8eb452..cef133e0513da64de8966850de192609afc3cff3 100644 (file)
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 72
+# stdlib_h.m4 serial 73
 dnl Copyright (C) 2007-2023 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -32,6 +32,49 @@ AC_DEFUN_ONCE([gl_STDLIB_H],
 
   AC_REQUIRE([AC_C_RESTRICT])
 
+  dnl Test whether MB_CUR_MAX needs to be overridden.
+  dnl On Solaris 10, in UTF-8 locales, its value is 3 but needs to be 4.
+  dnl Fortunately, we can do this because on this platform MB_LEN_MAX is 5.
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  AC_REQUIRE([gt_LOCALE_FR_UTF8])
+  AC_CACHE_CHECK([whether MB_CUR_MAX is correct],
+    [gl_cv_macro_MB_CUR_MAX_good],
+    [
+      dnl Initial guess, used when cross-compiling or when no suitable locale
+      dnl is present.
+changequote(,)dnl
+      case "$host_os" in
+                  # Guess no on Solaris.
+        solaris*) gl_cv_macro_MB_CUR_MAX_good="guessing no" ;;
+                  # Guess yes otherwise.
+        *)        gl_cv_macro_MB_CUR_MAX_good="guessing yes" ;;
+      esac
+changequote([,])dnl
+      if test $LOCALE_FR_UTF8 != none; then
+        AC_RUN_IFELSE(
+          [AC_LANG_SOURCE([[
+#include <locale.h>
+#include <stdlib.h>
+int main ()
+{
+  int result = 0;
+  if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
+    {
+      if (MB_CUR_MAX < 4)
+        result |= 1;
+    }
+  return result;
+}]])],
+          [gl_cv_macro_MB_CUR_MAX_good=yes],
+          [gl_cv_macro_MB_CUR_MAX_good=no]
+          [:])
+      fi
+    ])
+  case "$gl_cv_macro_MB_CUR_MAX_good" in
+    *yes) ;;
+    *) REPLACE_MB_CUR_MAX=1 ;;
+  esac
+
   AC_CHECK_DECLS_ONCE([ecvt])
   if test $ac_cv_have_decl_ecvt = no; then
     HAVE_DECL_ECVT=0
@@ -181,6 +224,7 @@ AC_DEFUN([gl_STDLIB_H_DEFAULTS],
   REPLACE_INITSTATE=0;       AC_SUBST([REPLACE_INITSTATE])
   REPLACE_MALLOC_FOR_MALLOC_GNU=0;    AC_SUBST([REPLACE_MALLOC_FOR_MALLOC_GNU])
   REPLACE_MALLOC_FOR_MALLOC_POSIX=0;  AC_SUBST([REPLACE_MALLOC_FOR_MALLOC_POSIX])
+  REPLACE_MB_CUR_MAX=0;      AC_SUBST([REPLACE_MB_CUR_MAX])
   REPLACE_MBSTOWCS=0;        AC_SUBST([REPLACE_MBSTOWCS])
   REPLACE_MBTOWC=0;          AC_SUBST([REPLACE_MBTOWC])
   REPLACE_MKOSTEMP=0;        AC_SUBST([REPLACE_MKOSTEMP])
index efea327794c035de565becb9c98121696ffd505b..25593a8a5d5e3884b5d764f5b07b320697508c03 100644 (file)
@@ -4,6 +4,7 @@ A GNU-like <stdlib.h>.
 Files:
 lib/stdlib.in.h
 m4/stdlib_h.m4
+m4/locale-fr.m4
 
 Depends-on:
 gen-header
@@ -141,6 +142,7 @@ stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
              -e 's|@''REPLACE_INITSTATE''@|$(REPLACE_INITSTATE)|g' \
              -e 's|@''REPLACE_MALLOC_FOR_MALLOC_GNU''@|$(REPLACE_MALLOC_FOR_MALLOC_GNU)|g' \
              -e 's|@''REPLACE_MALLOC_FOR_MALLOC_POSIX''@|$(REPLACE_MALLOC_FOR_MALLOC_POSIX)|g' \
+             -e 's|@''REPLACE_MB_CUR_MAX''@|$(REPLACE_MB_CUR_MAX)|g' \
              -e 's|@''REPLACE_MBSTOWCS''@|$(REPLACE_MBSTOWCS)|g' \
              -e 's|@''REPLACE_MBTOWC''@|$(REPLACE_MBTOWC)|g' \
              -e 's|@''REPLACE_MKOSTEMP''@|$(REPLACE_MKOSTEMP)|g' \