I did:
>       * lib/closedir.c: Include dirinfo-abstract.h instead of <unistd.h>.

Oops, this causes a crash of closedir() on MSVC: Seen as a 'test-glob' failure:

test-glob.c:63: res = glob ("./*", 0, NULL, &g);
glob.c:1100:    int status = glob_in_dir (filename, dirname, flags, errfunc, 
pglob, alloca_used);
glob.c:1508:    closedir (stream);
closedir.c:70:  close (fd);
=> Exception "An invalid parameter was passed"

The first of the attached patches fixes it. The second one is merely an
optimization.


2026-12-01  Bruno Haible  <[email protected]>

        closedir: Optimize.
        * lib/closedir.c (closedir): No need to call close or _gl_unregister_fd
        on a negative fd.

2026-12-01  Bruno Haible  <[email protected]>

        closedir: Fix crash on MSVC (regression 2026-11-25).
        * lib/closedir.c: Include <unistd.h>.
        * modules/closedir (Depends-on): Add 'close'.

>From e3358d868a6d26c624292dc3637ef6fad801e8ee Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Tue, 1 Dec 2026 22:37:02 +0100
Subject: [PATCH 1/2] closedir: Fix crash on MSVC (regression 2026-11-25).

* lib/closedir.c: Include <unistd.h>.
* modules/closedir (Depends-on): Add 'close'.
---
 ChangeLog        | 6 ++++++
 lib/closedir.c   | 1 +
 modules/closedir | 1 +
 3 files changed, 8 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 21b84ca4ca..00b765c77a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-12-01  Bruno Haible  <[email protected]>
+
+	closedir: Fix crash on MSVC (regression 2026-11-25).
+	* lib/closedir.c: Include <unistd.h>.
+	* modules/closedir (Depends-on): Add 'close'.
+
 2026-11-29  Bruno Haible  <[email protected]>
 
 	fchdir: Fix dependencies (regression 2026-11-25).
diff --git a/lib/closedir.c b/lib/closedir.c
index 62d993783d..69cfba9a33 100644
--- a/lib/closedir.c
+++ b/lib/closedir.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 
 #if REPLACE_FCHDIR
+# include <unistd.h> /* for close() */
 # include "dirinfo-abstract.h"
 #endif
 
diff --git a/modules/closedir b/modules/closedir
index f23072f24c..dafb8a4850 100644
--- a/modules/closedir
+++ b/modules/closedir
@@ -9,6 +9,7 @@ m4/closedir.m4
 Depends-on:
 dirent-h
 dirfd            [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
+close            [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
 dirinfo-abstract [test $HAVE_CLOSEDIR = 0 || test $REPLACE_CLOSEDIR = 1]
 
 configure.ac:
-- 
2.53.0

>From c4c962094331214d98d626596a7fd92bcc909c90 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Tue, 1 Dec 2026 22:39:17 +0100
Subject: [PATCH 2/2] closedir: Optimize.

* lib/closedir.c (closedir): No need to call close or _gl_unregister_fd
on a negative fd.
---
 ChangeLog      | 6 ++++++
 lib/closedir.c | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 00b765c77a..285075ffa3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2026-12-01  Bruno Haible  <[email protected]>
+
+	closedir: Optimize.
+	* lib/closedir.c (closedir): No need to call close or _gl_unregister_fd
+	on a negative fd.
+
 2026-12-01  Bruno Haible  <[email protected]>
 
 	closedir: Fix crash on MSVC (regression 2026-11-25).
diff --git a/lib/closedir.c b/lib/closedir.c
index 69cfba9a33..7321ca7189 100644
--- a/lib/closedir.c
+++ b/lib/closedir.c
@@ -67,10 +67,10 @@ closedir (DIR *dirp)
 #endif
 
 #if GNULIB_defined_DIR
-  if (retval >= 0)
+  if (retval >= 0 && fd >= 0)
     close (fd);
 #elif REPLACE_FCHDIR
-  if (retval >= 0)
+  if (retval >= 0 && fd >= 0)
     _gl_unregister_fd (fd);
 #endif
 
-- 
2.53.0

Reply via email to