]> Savannah Git Hosting - gnulib.git/commitdiff
stdlib: fix MB_CUR_MAX on older Android
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 23 Dec 2024 20:57:12 +0000 (12:57 -0800)
committerBruno Haible <bruno@clisp.org>
Wed, 25 Dec 2024 14:35:25 +0000 (15:35 +0100)
Android NDK r16 MB_CUR_MAX doesn’t link when compiling C.
Problem found in GNU Emacs, which worked around it this way:
https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=39a7e6b79fdeafc539a36f6831d922a2622cb679
... but this ran afoul of the recent Gnulib change that
added lib/stdlib.c.
* lib/stdlib.in.h (gl_MB_CUR_MAX): If @REPLACE_MB_CUR_MAX@
is positive, use its value directly.
* m4/stdlib_h.m4 (gl_STDLIB_H): Set REPLACE_MB_CUR_MAX to (-1)
if the Solaris bug, and to 4 if the Android bug.
* tests/test-stdlib.c (main): Check that MB_CUR_MAX compiles
and is nonzero.

ChangeLog
doc/posix-headers/stdlib.texi
lib/stdlib.in.h
m4/stdlib_h.m4
tests/test-stdlib.c

index be037c9c64df4485f0698611db2e48f919c500de..4c5ee6192d19500d766768ff3754119dfa84d9a2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2024-12-23  Paul Eggert  <eggert@cs.ucla.edu>
 
+       stdlib: fix MB_CUR_MAX on older Android
+       Android NDK r16 MB_CUR_MAX doesn’t link when compiling C.
+       Problem found in GNU Emacs, which worked around it this way:
+       https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=39a7e6b79fdeafc539a36f6831d922a2622cb679
+       ... but this ran afoul of the recent Gnulib change that
+       added lib/stdlib.c.
+       * lib/stdlib.in.h (gl_MB_CUR_MAX): If @REPLACE_MB_CUR_MAX@
+       is positive, use its value directly.
+       * m4/stdlib_h.m4 (gl_STDLIB_H): Set REPLACE_MB_CUR_MAX to (-1)
+       if the Solaris bug, and to 4 if the Android bug.
+       * tests/test-stdlib.c (main): Check that MB_CUR_MAX compiles
+       and is nonzero.
+
        stdlib: MB_CUR_MAX is type size_t
        * lib/stdlib.in.h (gl_MB_CUR_MAX): Return size_t, not int,
        to conform to POSIX.
index ac7efbda8465a446f52c777f0afaa104e55a5dea..bd6b8ec4d8f7d125586c64329551ff4196505d73 100644 (file)
@@ -15,6 +15,9 @@ Some platforms provide a @code{NULL} macro that cannot be used in arbitrary
 expressions:
 NetBSD 5.0
 @item
+Using the macro @code{MB_CUR_MAX} causes link errors on some platforms:
+Android NDK r16.
+@item
 The value of @code{MB_CUR_MAX} is too small (3 instead of 4) in UTF-8 locales
 on some platforms:
 Solaris 10.
index 975052b3c0ae335fcd17cbc669ca5d86d15872ef..4bb100cd8c497aa08ceb8dc8d8faefc8a03a19c1 100644 (file)
@@ -704,14 +704,19 @@ _GL_WARN_ON_USE (malloc, "malloc is not POSIX compliant everywhere - "
 # endif
 #endif
 
-/* Return maximum number of bytes of a multibyte character.  */
+/* Return maximum number of bytes in a multibyte character in the
+   current locale.  */
 #if @REPLACE_MB_CUR_MAX@
 # if !GNULIB_defined_MB_CUR_MAX
 static inline size_t
 gl_MB_CUR_MAX (void)
 {
+#  if 0 < @REPLACE_MB_CUR_MAX@
+  return @REPLACE_MB_CUR_MAX@;
+#  else
   /* Turn the value 3 to the value 4, as needed for the UTF-8 encoding.  */
   return MB_CUR_MAX + (MB_CUR_MAX == 3);
+#  endif
 }
 #  undef MB_CUR_MAX
 #  define MB_CUR_MAX gl_MB_CUR_MAX ()
index 92e67a74bb517c315760e58b402043212e1cc211..3baccaa885187d6b58dbb069b7304fe23907a873 100644 (file)
@@ -1,4 +1,4 @@
-# stdlib_h.m4 serial 76
+# stdlib_h.m4 serial 76.1
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -39,20 +39,24 @@ AC_DEFUN_ONCE([gl_STDLIB_H],
   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.
+    [AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM([[#include <stdlib.h>
+                        ]],
+                        [[return !!MB_CUR_MAX;]])
+       ],
+       [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
+        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([[
+        if test $LOCALE_FR_UTF8 != none; then
+          AC_RUN_IFELSE(
+            [AC_LANG_SOURCE([[
 #include <locale.h>
 #include <stdlib.h>
 int main ()
@@ -65,14 +69,21 @@ int main ()
     }
   return result;
 }]])],
-          [gl_cv_macro_MB_CUR_MAX_good=yes],
-          [gl_cv_macro_MB_CUR_MAX_good=no],
-          [:])
-      fi
+            [gl_cv_macro_MB_CUR_MAX_good=yes],
+            [gl_cv_macro_MB_CUR_MAX_good=no],
+            [:])
+        fi
+       ],
+       [gl_cv_macro_MB_CUR_MAX_good="link failed - so no"])
     ])
   case "$gl_cv_macro_MB_CUR_MAX_good" in
     *yes) ;;
-    *) REPLACE_MB_CUR_MAX=1 ;;
+    "link failed - so no")
+      # 4 suffices as a workaround in Android NDK 16,
+      # the only known platform with the bug.
+      REPLACE_MB_CUR_MAX=4
+      ;;
+    *) REPLACE_MB_CUR_MAX="(-1)" ;;
   esac
 
   AC_CHECK_DECLS_ONCE([ecvt])
index cb6db8028fe602f8344eb0f9ecd1559717581292..7b9cd50a794df29674c78b83ef86c577dda64218 100644 (file)
@@ -56,6 +56,9 @@ main (void)
     return 1;
 #endif
 
+  if (MB_CUR_MAX == 0)
+    return 1;
+
   if (test_sys_wait_macros ())
     return 2;