From 8624482160359112f6fc33e1a5408e998314ab68 Mon Sep 17 00:00:00 2001
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Tue, 4 Jun 2024 14:50:57 +0900
Subject: [PATCH] common:w32: Fix INEXTRA handling.

* common/exectool.c (gnupg_exec_tool_stream): On Windows, it's
HANDLE which a child process inherits.

--

GnuPG-bug-id: 7139
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
---
 common/exectool.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/common/exectool.c b/common/exectool.c
index 05504de98..d8326e77b 100644
--- a/common/exectool.c
+++ b/common/exectool.c
@@ -329,7 +329,11 @@ gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
   estream_t extrafp = NULL;
   estream_t outfp = NULL, errfp = NULL;
   es_poll_t fds[4];
+#ifdef HAVE_W32_SYSTEM
+  HANDLE exceptclose[2];
+#else
   int exceptclose[2];
+#endif
   int extrapipe[2] = {-1, -1};
   char extrafdbuf[20];
   const char *argsave = NULL;
@@ -339,6 +343,7 @@ gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
   struct copy_buffer *cpbuf_in = NULL, *cpbuf_out = NULL, *cpbuf_extra = NULL;
   int quiet = 0;
   gnupg_spawn_actions_t act = NULL;
+  int i = 0;
 
   memset (fds, 0, sizeof fds);
   memset (&fderrstate, 0, sizeof fderrstate);
@@ -392,16 +397,20 @@ gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
                      gpg_strerror (err));
           goto leave;
         }
-      exceptclose[0] = extrapipe[0]; /* Do not close in child. */
-      exceptclose[1] = -1;
+      /* Do not close in child. */
+#ifdef HAVE_W32_SYSTEM
+      exceptclose[i] = (HANDLE)_get_osfhandle (extrapipe[0]);
+#else
+      exceptclose[i] = extrapipe[0];
+#endif
       /* Now find the argument marker and replace by the pipe's fd.
          Yeah, that is an ugly non-thread safe hack but it safes us to
          create a copy of the array.  */
 #ifdef HAVE_W32_SYSTEM
       snprintf (extrafdbuf, sizeof extrafdbuf, "-&%lu",
-                (unsigned long)_get_osfhandle (extrapipe[0]));
+                (unsigned long)exceptclose[i]);
 #else
-      snprintf (extrafdbuf, sizeof extrafdbuf, "-&%d", extrapipe[0]);
+      snprintf (extrafdbuf, sizeof extrafdbuf, "-&%d", exceptclose[i]);
 #endif
       for (argsaveidx=0; argv[argsaveidx]; argsaveidx++)
         if (!strcmp (argv[argsaveidx], "-&@INEXTRA@"))
@@ -410,9 +419,14 @@ gnupg_exec_tool_stream (const char *pgmname, const char *argv[],
             argv[argsaveidx] = extrafdbuf;
             break;
           }
+      i++;
     }
-  else
-    exceptclose[0] = -1;
+
+#ifdef HAVE_W32_SYSTEM
+    exceptclose[i] = INVALID_HANDLE_VALUE;
+#else
+    exceptclose[i] = -1;
+#endif
 
   err = gnupg_spawn_actions_new (&act);
   if (err)