The existing logic for open() assumes a handle is always available in
the fdtable for a created file.  This leads to a situation where, if
there is no handle available, the file is created but cannot be
referenced by a Cygwin fd.

Update the code to check for an available handle before creating a file.

Reported-by: Christian Franke <[email protected]>
Addresses: https://cygwin.com/pipermail/cygwin/2026-May/259664.html
Signed-off-by: Mark Geisert <[email protected]>
Fixes: e859706578ba (* autoload.cc (NtCreateFile): Add.)

---
 winsup/cygwin/fhandler/base.cc | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/winsup/cygwin/fhandler/base.cc b/winsup/cygwin/fhandler/base.cc
index 5321ad7ff..d38669d5d 100644
--- a/winsup/cygwin/fhandler/base.cc
+++ b/winsup/cygwin/fhandler/base.cc
@@ -539,6 +539,12 @@ fhandler_base::open (int flags, mode_t mode)
   syscall_printf ("(%S, %y)%s", pc.get_nt_native_path (), flags,
                                get_handle () ? " by handle" : "");
 
+  /* If no handle is supplied, ensure an unused one is available */
+  if (!get_handle () && cygheap->fdtab.find_unused_handle () == -1)
+    {
+      /* errno has been set to EMFILE */
+      return res;
+    }
   if (flags & O_PATH)
     query_open (query_read_attributes);
 
-- 
2.51.0

Reply via email to