* src/paste.c: Include stdio--.h as per sc_require_stdio_safer.
Also avoid direct comparison of STDIN_FILENO which is more
accurate with closed stdin in combination with /dev/stdin.
* tests/paste/closed-stdio.sh: Add a test case.
* tests/local.mk: Reference the new test.
---
 src/paste.c                 | 14 ++++++------
 tests/local.mk              |  1 +
 tests/paste/closed-stdio.sh | 43 +++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 7 deletions(-)
 create mode 100755 tests/paste/closed-stdio.sh

diff --git a/src/paste.c b/src/paste.c
index abc65aa90..9d1c3d42b 100644
--- a/src/paste.c
+++ b/src/paste.c
@@ -37,12 +37,12 @@
 
 #include <config.h>
 
-#include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
 #include "system.h"
 #include "fadvise.h"
 #include "mcel.h"
+#include "stdio--.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "paste"
@@ -198,8 +198,7 @@ paste_parallel (size_t nfiles, char **fnamptr)
   /* Number of files still open to process.  */
   size_t files_open;
 
-  /* True if any fopen got fd == STDIN_FILENO.  */
-  bool opened_stdin = false;
+  size_t nstdin = 0;
 
   /* Attempt to open all files.  This could be expanded to an infinite
      number of files, but at the (considerable) expense of remembering
@@ -209,7 +208,7 @@ paste_parallel (size_t nfiles, char **fnamptr)
     {
       if (streq (fnamptr[files_open], "-"))
         {
-          have_read_stdin = true;
+          nstdin++;
           fileptr[files_open] = stdin;
         }
       else
@@ -217,13 +216,14 @@ paste_parallel (size_t nfiles, char **fnamptr)
           fileptr[files_open] = fopen (fnamptr[files_open], "r");
           if (fileptr[files_open] == NULL)
             error (EXIT_FAILURE, errno, "%s", quotef (fnamptr[files_open]));
-          else if (fileno (fileptr[files_open]) == STDIN_FILENO)
-            opened_stdin = true;
           fadvise (fileptr[files_open], FADVISE_SEQUENTIAL);
         }
     }
 
-  if (opened_stdin && have_read_stdin)
+  /* Diagnose a closed stdin before writing any columns from named files.  */
+  have_read_stdin = nstdin != 0;
+  if (have_read_stdin && nstdin < nfiles
+      && fcntl (STDIN_FILENO, F_GETFD) < 0 && errno == EBADF)
     error (EXIT_FAILURE, 0, _("standard input is closed"));
 
   /* Read a line from each file and output it to stdout separated by a
diff --git a/tests/local.mk b/tests/local.mk
index 5fe17818a..25a4127c1 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -433,6 +433,7 @@ all_tests =                                 \
   tests/od/od-x8.sh                            \
   tests/misc/pathchk.sh                                \
   tests/paste/paste.pl                         \
+  tests/paste/closed-stdio.sh                  \
   tests/paste/multi-byte.sh                    \
   tests/printf/printf.sh                       \
   tests/printf/printf-cov.pl                   \
diff --git a/tests/paste/closed-stdio.sh b/tests/paste/closed-stdio.sh
new file mode 100755
index 000000000..b1c7a9fda
--- /dev/null
+++ b/tests/paste/closed-stdio.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+# Test paste with standard input closed.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ paste
+
+echo a > in || framework_failure_
+paste in <&- > out 2> err || fail=1
+compare in out || fail=1
+compare /dev/null err || fail=1
+
+# Diagnose '-' before writing any columns from named files.
+echo 'paste: standard input is closed' > exp || framework_failure_
+for files in 'in -' '- in'; do
+  returns_ 1 paste $files <&- > out 2> err || fail=1
+  compare /dev/null out || fail=1
+  compare exp err || fail=1
+done
+
+# Opening 'in' must not make it available through /dev/stdin.
+if test "$(cat /dev/stdin < in 2>/dev/null)" = a &&
+   returns_ 1 paste /dev/stdin <&- > /dev/null 2> exp; then
+  returns_ 1 paste in /dev/stdin <&- > out 2> err || fail=1
+  compare /dev/null out || fail=1
+  compare exp err || fail=1
+fi
+
+Exit $fail
-- 
2.56.0


Reply via email to