From 4c7a3798d8f04a153a3c15d7cf4f6f7511f4b74f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 16 Jun 2023 16:34:19 -0700 Subject: [PATCH] =?utf8?q?Use=20Gnulib=20=E2=80=98dup2=E2=80=99=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 1 + src/system.c | 21 +++++++-------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/gnulib.modules b/gnulib.modules index 6fc9b2cc..84732f5b 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -28,6 +28,7 @@ backupfile closeout configmake dirname +dup2 error exclude extern-inline diff --git a/src/system.c b/src/system.c index 2988ed08..93fff885 100644 --- a/src/system.c +++ b/src/system.c @@ -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); } -- 2.47.2