From c386ed6eb0e385fa1fff33a8c1d0115c6dda9e01 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Wed, 23 Mar 2022 10:22:51 -0700
Subject: [PATCH] glob: test for glibc bug 25659

https://sourceware.org/bugzilla/show_bug.cgi?id=25659
* m4/glob.m4 (gl_GLOB): Replace glob if it has bug 25659.
* tests/test-glob.c (main): Test for glibc bug 25659.
---
 ChangeLog         |  5 +++++
 m4/glob.m4        | 41 ++++++++++++++++++++++++++++++++++++++++-
 tests/test-glob.c |  9 ++++++++-
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a0d3519162..7ea4f7797b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2022-03-23  Paul Eggert  <eggert@cs.ucla.edu>
 
+	glob: test for glibc bug 25659
+	https://sourceware.org/bugzilla/show_bug.cgi?id=25659
+	* m4/glob.m4 (gl_GLOB): Replace glob if it has bug 25659.
+	* tests/test-glob.c (main): Test for glibc bug 25659.
+
 	glob: fix symlink and // issues; improve speed
 	* lib/glob.c: Include fcntl.h.
 	(dirfd) [_LIBC]: New macro.
diff --git a/m4/glob.m4 b/m4/glob.m4
index 0d1426389b..cf5f93930c 100644
--- a/m4/glob.m4
+++ b/m4/glob.m4
@@ -1,4 +1,4 @@
-# glob.m4 serial 24
+# glob.m4 serial 25
 dnl Copyright (C) 2005-2007, 2009-2022 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -66,6 +66,45 @@ char a[_GNU_GLOB_INTERFACE_VERSION == 1 || _GNU_GLOB_INTERFACE_VERSION == 2 ? 1
       esac
     fi
 
+    if test $REPLACE_GLOB = 0; then
+      AC_CACHE_CHECK([whether glob NOTDIR*/ omits symlink to nondir],
+                     [gl_cv_glob_omit_nondir_symlinks],
+        [if test $cross_compiling != yes; then
+           if ln -s conf-file conf$$-globtest 2>/dev/null && touch conf-file
+           then
+             gl_cv_glob_omit_nondir_symlinks=maybe
+           else
+             # If we can't make a symlink, then we cannot test this issue.  Be
+             # pessimistic about this.
+             gl_cv_glob_omit_nondir_symlinks=no
+           fi
+           if test $gl_cv_glob_omit_nondir_symlinks = maybe; then
+             AC_RUN_IFELSE(
+               [AC_LANG_PROGRAM(
+                  [[#include <stddef.h>
+                    #include <glob.h>]],
+                  [[glob_t found;
+                    if (glob ("conf*-globtest/", 0, NULL, &found) != GLOB_NOMATCH)
+                      return 1;
+                    globfree (&found);
+                  ]])],
+               [gl_cv_glob_omit_nondir_symlinks=yes],
+               [gl_cv_glob_omit_nondir_symlinks=no],
+               [dnl We don't get here.
+                :
+               ])
+           fi
+           rm -f conf$$-globtest
+         else
+           gl_cv_glob_omit_nondir_symlinks="$gl_cross_guess_normal"
+         fi
+        ])
+      case "$gl_cv_glob_omit_nondir_symlinks" in
+        *yes) ;;
+        *) REPLACE_GLOB=1 ;;
+      esac
+    fi
+
   fi
 
   if test $ac_cv_func_glob_pattern_p = no; then
diff --git a/tests/test-glob.c b/tests/test-glob.c
index 2709080bc7..568acf14b7 100644
--- a/tests/test-glob.c
+++ b/tests/test-glob.c
@@ -72,7 +72,9 @@ main ()
   globfree (&g);
 
   if ((symlink (GL_NO_SUCH_FILE, BASE "globlink1") == 0 || errno == EEXIST)
-      && (symlink (".", BASE "globlink2") == 0 || errno == EEXIST))
+      && (symlink (".", BASE "globlink2") == 0 || errno == EEXIST)
+      && (symlink (BASE "globfile", BASE "globlink3") == 0 || errno == EEXIST)
+      && close (creat (BASE "globfile", 0666)) == 0)
     {
       res = glob (BASE "globlink[12]", 0, NULL, &g);
       ASSERT (res == 0 && g.gl_pathc == 2);
@@ -80,6 +82,11 @@ main ()
       ASSERT (strcmp (g.gl_pathv[1], BASE "globlink2") == 0);
       globfree (&g);
 
+      res = glob (BASE "globlink[123]/", 0, NULL, &g);
+      ASSERT (res == 0 && g.gl_pathc == 1);
+      ASSERT (strcmp (g.gl_pathv[0], BASE "globlink2/") == 0);
+      globfree (&g);
+
       res = glob (BASE "globlink[12]", GLOB_MARK, NULL, &g);
       ASSERT (res == 0 && g.gl_pathc == 2);
       ASSERT (strcmp (g.gl_pathv[0], BASE "globlink1") == 0);
-- 
2.39.5