> We could detect that archive contents are coming from terminal input and
> fail immediately (IIRC tar waits indefinitely ATM until it's input is cut
> by ctrl+d or it is killed).

I meant something like the patch attached, could you please consider?

I tried to write testcase for this and I failed to find proper 'script'
alternative tool among coreutils, is there some?  I could rewrite it then.

Pavel
>From 57a95aff7d9ff9f257d934f823b7e4b1ae276caf Mon Sep 17 00:00:00 2001
From: Pavel Raiskup <[email protected]>
Date: Thu, 13 Mar 2014 08:58:42 +0100
Subject: [PATCH] tar: fail if archive comes directly to/from terminal

References:
http://lists.gnu.org/archive/html/bug-tar/2014-03/msg00037.html

* src/buffer.c (_open_archive): Check if isatty() and fail if yes.
* tests/iotty.at: New testcase.
* tests/Makefile.am: Handle new testcase.
* tests/testsuite.at: Handle new testcase and define new macro
AT_SCRIPT_PREREQ.
---
 src/buffer.c       |  9 +++++++++
 tests/Makefile.am  |  1 +
 tests/iotty.at     | 45 +++++++++++++++++++++++++++++++++++++++++++++
 tests/testsuite.at |  7 +++++++
 4 files changed, 62 insertions(+)
 create mode 100644 tests/iotty.at

diff --git a/src/buffer.c b/src/buffer.c
index 95e8a26..9eb1434 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -732,6 +732,11 @@ _open_archive (enum access_mode wanted_access)
 
             archive = STDIN_FILENO;
 
+            if (isatty (archive))
+              FATAL_ERROR ((0, 0,
+                            _("Reading archive contents from terminal "
+                              "input is not possible (missing -f option?)")));
+
             type = check_compressed_archive (&shortfile);
             if (type != ct_tar && type != ct_none)
               FATAL_ERROR ((0, 0,
@@ -744,6 +749,10 @@ _open_archive (enum access_mode wanted_access)
 
         case ACCESS_WRITE:
           archive = STDOUT_FILENO;
+          if (isatty (archive))
+            FATAL_ERROR ((0, 0,
+                          _("Writing archive contents to terminal "
+                            "output is not possible (missing -f option?)")));
           if (!index_file_name)
             stdlis = stderr;
           break;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d4c9362..0ab28c8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -116,6 +116,7 @@ TESTSUITE_AT = \
  incr09.at\
  indexfile.at\
  ignfail.at\
+ iotty.at\
  label01.at\
  label02.at\
  label03.at\
diff --git a/tests/iotty.at b/tests/iotty.at
new file mode 100644
index 0000000..f89691c
--- /dev/null
+++ b/tests/iotty.at
@@ -0,0 +1,45 @@
+# Process this file with autom4te to create testsuite. -*- Autotest -*-
+
+# Test suite for GNU tar.
+# Copyright 2014 Free Software Foundation, Inc.
+
+# This file is part of GNU tar.
+
+# GNU tar 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.
+
+# GNU tar 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 <http://www.gnu.org/licenses/>.
+
+# Ensure that we may not put/read archive contents to/from terminal
+# output/input.
+#
+# Original report:
+# http://lists.gnu.org/archive/html/bug-tar/2014-03/msg00033.html
+
+AT_SETUP([terminal input/output])
+AT_KEYWORDS([options iotty])
+
+AT_TAR_CHECK([
+AT_SCRIPT_PREREQ
+genfile --file file
+script -q -c "tar -c f" /dev/null > out
+script -q -c "tar -x"   /dev/null >> out
+cat out | sed 's|\r||'
+],
+[0],
+[tar: Writing archive contents to terminal output is not possible (missing -f option?)
+tar: Error is not recoverable: exiting now
+tar: Reading archive contents from terminal input is not possible (missing -f option?)
+tar: Error is not recoverable: exiting now
+],
+[],[],[],[posix, gnu, oldgnu])
+
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index c52890b..2011e4a 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -124,6 +124,12 @@ m4_define([AT_CHECK_UTIL],[
   fi
 ])
 
+m4_define([AT_SCRIPT_PREREQ],[
+  echo "sth" > expected
+  script -q -c "echo sth" /dev/null | sed 's|\r||' > output
+  rm -rf output expected
+])
+
 m4_define([AT_XATTRS_UTILS_PREREQ],[
   file=$(TMPDIR=. mktemp fiXXXXXX)
   AT_CHECK_UTIL(setfattr -n user.test -v test $file,0)
@@ -213,6 +219,7 @@ m4_include([gzip.at])
 m4_include([recurse.at])
 m4_include([recurs02.at])
 m4_include([shortrec.at])
+m4_include([iotty.at])
 
 AT_BANNER([The --same-order option])
 m4_include([same-order01.at])
-- 
1.8.5.3

Reply via email to