Hi Collin,

> On Android compiling a testdir in termux results in test-glob failing
> with the following:
> 
>     $ ./gltests/test-glob
>     test-glob.c:118: assertion 'res == 0' failed
>     Aborted                    ./gltests/test-glob
> 
> I think this is because the shell is at /system/bin/sh instead of
> /bin/sh.

Yes, this comes from the absence of /bin/sh (cf. m4/sh-filename.m4).

> How about accepting GLOB_NOMATCH in this case? Since the main purpose of
> this test is to catch the glibc stack overflow.

This patch has three problems:
  1) It weakens the test also on glibc systems.
  2) No, the purpose of the test is also to catch wrong exit codes without
     stack overflow, like on Cygwin.
  3) m4/glob.m4 needs to adapted as well.

Even after excluding that part of the test from m4/glob.m4, it fails.
Namely, for n >= 4097, this test fails:
===============================================================================
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>
int
main (void)
{
  /* Test that glob with many trailing slashes or slashes following
     a wildcard does not overflow the stack as it did in glibc 2.42
     and earlier.  */
  int n = 4097;
  char *p = malloc (n);
  glob_t g;
  int result = 0;
  if (p != NULL)
    {
      /* This test fails on glibc <= 2.42 (stack overflow).  */
      memset (p, '/', n-1);
      p[n-1] = '\0';
      int ret = glob (p, 0, NULL, &g);
      printf ("ret = %d\n", ret);
      if (ret != 0)
        result |= 1;
      globfree (&g);
    }
  return result;
}
===============================================================================

Therefore I'm applying this patch:


2025-10-20  Bruno Haible  <[email protected]>

        glob: Add support for Android.
        Reported by Collin Funk in
        <https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00054.html>.
        * m4/glob.m4 (gl_GLOB): On Android, skip a test that assumes that
        /bin/sh exists. Update cross-compilation guess.
        * tests/test-glob.c (main): On Android, skip a test that assumes that
        /bin/sh exists.
        * doc/posix-functions/glob.texi: Mention also Android.

diff --git a/doc/posix-functions/glob.texi b/doc/posix-functions/glob.texi
index d3cc89442e..e3020e8da7 100644
--- a/doc/posix-functions/glob.texi
+++ b/doc/posix-functions/glob.texi
@@ -27,7 +27,7 @@
 @c https://sourceware.org/PR30635
 @c https://sourceware.org/PR33537
 glibc 2.42,
-Cygwin.
+Cygwin, Android.
 @end itemize
 
 Portability problems not fixed by Gnulib:
diff --git a/m4/glob.m4 b/m4/glob.m4
index 77b1cc8380..854fcf579c 100644
--- a/m4/glob.m4
+++ b/m4/glob.m4
@@ -1,5 +1,5 @@
 # glob.m4
-# serial 33
+# serial 34
 dnl Copyright (C) 2005-2007, 2009-2025 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -42,12 +42,14 @@ AC_DEFUN([gl_GLOB]
               int result = 0;
               if (p != NULL)
                 {
-                  /* This test fails on glibc <= 2.42 (stack overflow).  */
+                  /* This test fails on glibc <= 2.42 (stack overflow),
+                     Android.  */
                   memset (p, '/', 9999);
                   p[9999] = '\0';
                   if (glob (p, 0, NULL, &g) != 0)
                     result |= 1;
                   globfree (&g);
+                  #if !defined __ANDROID__
                   /* This test fails on Cygwin 3.6.4 (return value
                      GLOB_ABORTED).  */
                   memset (p, '/', 9997);
@@ -56,18 +58,21 @@ AC_DEFUN([gl_GLOB]
                   if (glob (p, 0, NULL, &g) != 0)
                     result |= 2;
                   globfree (&g);
+                  #endif
                 }
               return result;
             }]])],
          [gl_cv_glob_overflows_stack=no],
          [gl_cv_glob_overflows_stack=yes],
          [case "$host_os" in
-                           # Guess yes on glibc systems.
-            *-gnu* | gnu*) gl_cv_glob_overflows_stack="guessing yes" ;;
-                           # Guess yes on Cygwin.
-            cygwin*)       gl_cv_glob_overflows_stack="guessing yes" ;;
-                           # If we don't know, obey --enable-cross-guesses.
-            *)             
gl_cv_glob_overflows_stack="$gl_cross_guess_inverted" ;;
+                             # Guess yes on glibc systems.
+            *-gnu* | gnu*)   gl_cv_glob_overflows_stack="guessing yes" ;;
+                             # Guess yes on Cygwin.
+            cygwin*)         gl_cv_glob_overflows_stack="guessing yes" ;;
+                             # Guess yes on Android.
+            linux*-android*) gl_cv_glob_overflows_stack="guessing yes" ;;
+                             # If we don't know, obey --enable-cross-guesses.
+            *)               
gl_cv_glob_overflows_stack="$gl_cross_guess_inverted" ;;
           esac
          ])
       ])
diff --git a/tests/test-glob.c b/tests/test-glob.c
index 23dda01291..5104ac94eb 100644
--- a/tests/test-glob.c
+++ b/tests/test-glob.c
@@ -110,6 +110,8 @@ main ()
       ASSERT (res == 0);
       globfree (&g);
 
+      /* On Android, /bin/sh does not exist.  It's /system/bin/sh instead.  */
+# if !defined __ANDROID__
       /* "/*/////sh".  */
       memset (pattern, '/', 9997);
       pattern[1] = '*';
@@ -117,6 +119,7 @@ main ()
       res = glob (pattern, 0, NULL, &g);
       ASSERT (res == 0);
       globfree (&g);
+# endif
 
       free (pattern);
     }




Reply via email to