Petr Šabata 33fc6c
From 9e49d343e3cd7e20dad1b86ebfb764e8027596a7 Mon Sep 17 00:00:00 2001
Petr Šabata 33fc6c
From: Chet Ramey <chet.ramey@case.edu>
Petr Šabata 33fc6c
Date: Fri, 24 Apr 2020 11:05:06 -0400
Petr Šabata 33fc6c
Subject: [PATCH] Bash-5.0 patch 17: better fix for reaping process
Petr Šabata 33fc6c
 substitution file descriptors
Petr Šabata 33fc6c
Petr Šabata 33fc6c
---
Petr Šabata 33fc6c
 execute_cmd.c | 26 ++++++--------------------
Petr Šabata 33fc6c
 patchlevel.h  |  2 +-
Petr Šabata 33fc6c
 subst.c       | 35 +++++++++++++++++++++--------------
Petr Šabata 33fc6c
 subst.h       |  5 ++---
Petr Šabata 33fc6c
 4 files changed, 30 insertions(+), 38 deletions(-)
Petr Šabata 33fc6c
Petr Šabata 33fc6c
diff --git a/execute_cmd.c b/execute_cmd.c
Petr Šabata 33fc6c
index 3864986d..4a05758d 100644
Petr Šabata 33fc6c
--- a/execute_cmd.c
Petr Šabata 33fc6c
+++ b/execute_cmd.c
Petr Šabata 33fc6c
@@ -564,7 +564,7 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Petr Šabata 33fc6c
   volatile int save_line_number;
Petr Šabata 33fc6c
 #if defined (PROCESS_SUBSTITUTION)
Petr Šabata 33fc6c
   volatile int ofifo, nfifo, osize, saved_fifo;
Petr Šabata 33fc6c
-  volatile char *ofifo_list;
Petr Šabata 33fc6c
+  volatile void *ofifo_list;
Petr Šabata 33fc6c
 #endif
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (breaking || continuing)
Petr Šabata 33fc6c
@@ -750,12 +750,14 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Petr Šabata 33fc6c
   reap_procsubs ();
Petr Šabata 33fc6c
 #  endif
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-  if (variable_context != 0)	/* XXX - also if sourcelevel != 0? */
Petr Šabata 33fc6c
+  /* XXX - also if sourcelevel != 0? */
Petr Šabata 33fc6c
+  if (variable_context != 0)
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
       ofifo = num_fifos ();
Petr Šabata 33fc6c
       ofifo_list = copy_fifo_list ((int *)&osize);
Petr Šabata 33fc6c
       begin_unwind_frame ("internal_fifos");
Petr Šabata 33fc6c
-      add_unwind_protect (xfree, ofifo_list);
Petr Šabata 33fc6c
+      if (ofifo_list)
Petr Šabata 33fc6c
+	add_unwind_protect (xfree, ofifo_list);
Petr Šabata 33fc6c
       saved_fifo = 1;
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
   else
Petr Šabata 33fc6c
@@ -1099,26 +1101,10 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
       nfifo = num_fifos ();
Petr Šabata 33fc6c
       if (nfifo > ofifo)
Petr Šabata 33fc6c
-	close_new_fifos ((char *)ofifo_list, osize);
Petr Šabata 33fc6c
+	close_new_fifos ((void *)ofifo_list, osize);
Petr Šabata 33fc6c
       free ((void *)ofifo_list);
Petr Šabata 33fc6c
       discard_unwind_frame ("internal_fifos");
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
-# if defined (HAVE_DEV_FD)
Petr Šabata 33fc6c
-  /* Reap process substitutions at the end of loops */
Petr Šabata 33fc6c
-  switch (command->type)
Petr Šabata 33fc6c
-    {
Petr Šabata 33fc6c
-    case cm_while:
Petr Šabata 33fc6c
-    case cm_until:
Petr Šabata 33fc6c
-    case cm_for:
Petr Šabata 33fc6c
-    case cm_group:
Petr Šabata 33fc6c
-#    if defined (ARITH_FOR_COMMAND)
Petr Šabata 33fc6c
-    case cm_arith_for:
Petr Šabata 33fc6c
-#    endif
Petr Šabata 33fc6c
-      reap_procsubs ();
Petr Šabata 33fc6c
-    default:
Petr Šabata 33fc6c
-      break;
Petr Šabata 33fc6c
-    }
Petr Šabata 33fc6c
-#  endif /* HAVE_DEV_FD */
Petr Šabata 33fc6c
 #endif
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   /* Invert the return value if we have to */
Petr Šabata 33fc6c
diff --git a/patchlevel.h b/patchlevel.h
Petr Šabata 33fc6c
index 9074f4dd..98e714da 100644
Petr Šabata 33fc6c
--- a/patchlevel.h
Petr Šabata 33fc6c
+++ b/patchlevel.h
Petr Šabata 33fc6c
@@ -25,6 +25,6 @@
Petr Šabata 33fc6c
    regexp `^#define[ 	]*PATCHLEVEL', since that's what support/mkversion.sh
Petr Šabata 33fc6c
    looks for to find the patch level (for the sccs version string). */
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-#define PATCHLEVEL 16
Petr Šabata 33fc6c
+#define PATCHLEVEL 17
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #endif /* _PATCHLEVEL_H_ */
Petr Šabata 33fc6c
diff --git a/subst.c b/subst.c
Petr Šabata 33fc6c
index fd6db240..8884b487 100644
Petr Šabata 33fc6c
--- a/subst.c
Petr Šabata 33fc6c
+++ b/subst.c
Petr Šabata 33fc6c
@@ -5336,13 +5336,13 @@ clear_fifo_list ()
Petr Šabata 33fc6c
 {
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-char *
Petr Šabata 33fc6c
+void *
Petr Šabata 33fc6c
 copy_fifo_list (sizep)
Petr Šabata 33fc6c
      int *sizep;
Petr Šabata 33fc6c
 {
Petr Šabata 33fc6c
   if (sizep)
Petr Šabata 33fc6c
     *sizep = 0;
Petr Šabata 33fc6c
-  return (char *)NULL;
Petr Šabata 33fc6c
+  return (void *)NULL;
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 static void
Petr Šabata 33fc6c
@@ -5408,8 +5408,13 @@ unlink_fifo_list ()
Petr Šabata 33fc6c
       for (i = j = 0; i < nfifo; i++)
Petr Šabata 33fc6c
 	if (fifo_list[i].file)
Petr Šabata 33fc6c
 	  {
Petr Šabata 33fc6c
-	    fifo_list[j].file = fifo_list[i].file;
Petr Šabata 33fc6c
-	    fifo_list[j].proc = fifo_list[i].proc;
Petr Šabata 33fc6c
+	    if (i != j)
Petr Šabata 33fc6c
+	      {
Petr Šabata 33fc6c
+		fifo_list[j].file = fifo_list[i].file;
Petr Šabata 33fc6c
+		fifo_list[j].proc = fifo_list[i].proc;
Petr Šabata 33fc6c
+		fifo_list[i].file = (char *)NULL;
Petr Šabata 33fc6c
+		fifo_list[i].proc = 0;
Petr Šabata 33fc6c
+	      }
Petr Šabata 33fc6c
 	    j++;
Petr Šabata 33fc6c
 	  }
Petr Šabata 33fc6c
       nfifo = j;
Petr Šabata 33fc6c
@@ -5425,10 +5430,11 @@ unlink_fifo_list ()
Petr Šabata 33fc6c
    case it's larger than fifo_list_size (size of fifo_list). */
Petr Šabata 33fc6c
 void
Petr Šabata 33fc6c
 close_new_fifos (list, lsize)
Petr Šabata 33fc6c
-     char *list;
Petr Šabata 33fc6c
+     void *list;
Petr Šabata 33fc6c
      int lsize;
Petr Šabata 33fc6c
 {
Petr Šabata 33fc6c
   int i;
Petr Šabata 33fc6c
+  char *plist;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (list == 0)
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
@@ -5436,8 +5442,8 @@ close_new_fifos (list, lsize)
Petr Šabata 33fc6c
       return;
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-  for (i = 0; i < lsize; i++)
Petr Šabata 33fc6c
-    if (list[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
Petr Šabata 33fc6c
+  for (plist = (char *)list, i = 0; i < lsize; i++)
Petr Šabata 33fc6c
+    if (plist[i] == 0 && i < fifo_list_size && fifo_list[i].proc != -1)
Petr Šabata 33fc6c
       unlink_fifo (i);
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   for (i = lsize; i < fifo_list_size; i++)
Petr Šabata 33fc6c
@@ -5559,22 +5565,22 @@ clear_fifo_list ()
Petr Šabata 33fc6c
   nfds = 0;
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-char *
Petr Šabata 33fc6c
+void *
Petr Šabata 33fc6c
 copy_fifo_list (sizep)
Petr Šabata 33fc6c
      int *sizep;
Petr Šabata 33fc6c
 {
Petr Šabata 33fc6c
-  char *ret;
Petr Šabata 33fc6c
+  void *ret;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (nfds == 0 || totfds == 0)
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
       if (sizep)
Petr Šabata 33fc6c
 	*sizep = 0;
Petr Šabata 33fc6c
-      return (char *)NULL;
Petr Šabata 33fc6c
+      return (void *)NULL;
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (sizep)
Petr Šabata 33fc6c
     *sizep = totfds;
Petr Šabata 33fc6c
-  ret = (char *)xmalloc (totfds * sizeof (pid_t));
Petr Šabata 33fc6c
+  ret = xmalloc (totfds * sizeof (pid_t));
Petr Šabata 33fc6c
   return (memcpy (ret, dev_fd_list, totfds * sizeof (pid_t)));
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
@@ -5647,10 +5653,11 @@ unlink_fifo_list ()
Petr Šabata 33fc6c
    totfds (size of dev_fd_list). */
Petr Šabata 33fc6c
 void
Petr Šabata 33fc6c
 close_new_fifos (list, lsize)
Petr Šabata 33fc6c
-     char *list;
Petr Šabata 33fc6c
+     void *list;
Petr Šabata 33fc6c
      int lsize;
Petr Šabata 33fc6c
 {
Petr Šabata 33fc6c
   int i;
Petr Šabata 33fc6c
+  pid_t *plist;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (list == 0)
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
@@ -5658,8 +5665,8 @@ close_new_fifos (list, lsize)
Petr Šabata 33fc6c
       return;
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-  for (i = 0; i < lsize; i++)
Petr Šabata 33fc6c
-    if (list[i] == 0 && i < totfds && dev_fd_list[i])
Petr Šabata 33fc6c
+  for (plist = (pid_t *)list, i = 0; i < lsize; i++)
Petr Šabata 33fc6c
+    if (plist[i] == 0 && i < totfds && dev_fd_list[i])
Petr Šabata 33fc6c
       unlink_fifo (i);
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   for (i = lsize; i < totfds; i++)
Petr Šabata 33fc6c
diff --git a/subst.h b/subst.h
Petr Šabata 33fc6c
index 34763222..faf831bd 100644
Petr Šabata 33fc6c
--- a/subst.h
Petr Šabata 33fc6c
+++ b/subst.h
Petr Šabata 33fc6c
@@ -273,9 +273,8 @@ extern int num_fifos __P((void));
Petr Šabata 33fc6c
 extern void unlink_fifo_list __P((void));
Petr Šabata 33fc6c
 extern void unlink_fifo __P((int));
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-extern char *copy_fifo_list __P((int *));
Petr Šabata 33fc6c
-extern void unlink_new_fifos __P((char *, int));
Petr Šabata 33fc6c
-extern void close_new_fifos __P((char *, int));
Petr Šabata 33fc6c
+extern void *copy_fifo_list __P((int *));
Petr Šabata 33fc6c
+extern void close_new_fifos __P((void *, int));
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 extern void clear_fifo_list __P((void));
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-- 
Petr Šabata 33fc6c
2.25.1
Petr Šabata 33fc6c