87c1f6
commit 75fe6d1a1620d84e0e487868feba9b2c0f109610
87c1f6
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
87c1f6
Date:   Wed May 12 10:13:41 2021 +0530
87c1f6
87c1f6
    support: Close fds in copy_func
87c1f6
    
87c1f6
    copy_func may leave file descriptors open on error, so close them on
87c1f6
    function exit.
87c1f6
87c1f6
diff --git a/support/shell-container.c b/support/shell-container.c
87c1f6
index 3869e14683fb74dd..f0a9814ae230d167 100644
87c1f6
--- a/support/shell-container.c
87c1f6
+++ b/support/shell-container.c
87c1f6
@@ -93,8 +93,9 @@ copy_func (char **argv)
87c1f6
 {
87c1f6
   char *sname = argv[0];
87c1f6
   char *dname = argv[1];
87c1f6
-  int sfd, dfd;
87c1f6
+  int sfd = -1, dfd = -1;
87c1f6
   struct stat st;
87c1f6
+  int ret = 1;
87c1f6
 
87c1f6
   sfd = open (sname, O_RDONLY);
87c1f6
   if (sfd < 0)
87c1f6
@@ -108,7 +109,7 @@ copy_func (char **argv)
87c1f6
     {
87c1f6
       fprintf (stderr, "cp: unable to fstat %s: %s\n",
87c1f6
 	       sname, strerror (errno));
87c1f6
-      return 1;
87c1f6
+      goto out;
87c1f6
     }
87c1f6
 
87c1f6
   dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
87c1f6
@@ -116,22 +117,26 @@ copy_func (char **argv)
87c1f6
     {
87c1f6
       fprintf (stderr, "cp: unable to open %s for writing: %s\n",
87c1f6
 	       dname, strerror (errno));
87c1f6
-      return 1;
87c1f6
+      goto out;
87c1f6
     }
87c1f6
 
87c1f6
   if (support_copy_file_range (sfd, 0, dfd, 0, st.st_size, 0) != st.st_size)
87c1f6
     {
87c1f6
       fprintf (stderr, "cp: cannot copy file %s to %s: %s\n",
87c1f6
 	       sname, dname, strerror (errno));
87c1f6
-      return 1;
87c1f6
+      goto out;
87c1f6
     }
87c1f6
 
87c1f6
-  close (sfd);
87c1f6
-  close (dfd);
87c1f6
-
87c1f6
+  ret = 0;
87c1f6
   chmod (dname, st.st_mode & 0777);
87c1f6
 
87c1f6
-  return 0;
87c1f6
+out:
87c1f6
+  if (sfd >= 0)
87c1f6
+    close (sfd);
87c1f6
+  if (dfd >= 0)
87c1f6
+    close (dfd);
87c1f6
+
87c1f6
+  return ret;
87c1f6
 
87c1f6
 }
87c1f6