]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Use Gnulib ‘dup2’ module
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 16 Jun 2023 23:34:19 +0000 (16:34 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 17 Jun 2023 00:12:40 +0000 (17:12 -0700)
This simplifies code that would otherwise use dup and close.
* gnulib.modules: Add dup2.
* src/system.c: Add #pragma to pacify GCC 13.
(xdup2): Simplify by using dup2.

gnulib.modules
src/system.c

index 6fc9b2cc3f27015c0fab374ef1d6813851418d4f..84732f5b834f2300296f36ff382ad88b8cb4523a 100644 (file)
@@ -28,6 +28,7 @@ backupfile
 closeout
 configmake
 dirname
+dup2
 error
 exclude
 extern-inline
index 2988ed08cbfda2229fd68c5cefd631510a43dc30..93fff885aca324352bb5b91372c8889974afdd32 100644 (file)
@@ -299,6 +299,11 @@ sys_write_archive_buffer (void)
 #define        PREAD 0                 /* read file descriptor from pipe() */
 #define        PWRITE 1                /* write file descriptor from pipe() */
 
+/* Work around GCC bug 109839.  */
+#if 13 <= __GNUC__
+# pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
+#endif
+
 /* Duplicate file descriptor FROM into becoming INTO.
    INTO is closed first and has to be the next available slot.  */
 static void
@@ -306,22 +311,10 @@ xdup2 (int from, int into)
 {
   if (from != into)
     {
-      int status = close (into);
-
-      if (status != 0 && errno != EBADF)
+      if (dup2 (from, into) < 0)
        {
          int e = errno;
-         FATAL_ERROR ((0, e, _("Cannot close")));
-       }
-      status = dup (from);
-      if (status != into)
-       {
-         if (status < 0)
-           {
-             int e = errno;
-             FATAL_ERROR ((0, e, _("Cannot dup")));
-           }
-         abort ();
+         FATAL_ERROR ((0, e, _("Cannot dup2")));
        }
       xclose (from);
     }