summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@linbit.com>2011-02-03 23:07:15 +0100
committerAndreas Gruenbacher <agruen@linbit.com>2011-02-03 23:22:56 +0100
commit3c776b08449581b44afddeea68942531d2f047b2 (patch)
tree508b45a2bd3598c9bc31624203056085df496a95
parent4c3004c17fa72b5b87a1eec29ad41cd6549d0017 (diff)
downloadpatch-3c776b08449581b44afddeea68942531d2f047b2.tar.gz
Also check if the input file is seekable if a filename is given (-i)
* src/pch.c (open_patch_file): Also check if the input file is seekable if a filename is given (-i). * tests/pipe: New file. Test this. * tests/Makefile.am (TESTS): Add it.
-rw-r--r--ChangeLog8
-rw-r--r--src/pch.c74
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/fifo42
4 files changed, 86 insertions, 39 deletions
diff --git a/ChangeLog b/ChangeLog
index c213230..bfb0d3f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-02-03 Andreas Gruenbacher <agruen@linbit.com>
+
+ Also check if the input file is seekable if a filename is given (-i)
+ * src/pch.c (open_patch_file): Also check if the input file is
+ seekable if a filename is given (-i).
+ * tests/pipe: New file. Test this.
+ * tests/Makefile.am (TESTS): Add it.
+
2011-02-03 Jim Meyering <meyering@redhat.com>
doc: mention the fix for CVE-2010-4651
diff --git a/src/pch.c b/src/pch.c
index 8e64298..68f7bc8 100644
--- a/src/pch.c
+++ b/src/pch.c
@@ -106,52 +106,48 @@ void
open_patch_file (char const *filename)
{
file_offset file_pos = 0;
+ file_offset pos;
struct stat st;
+
if (!filename || !*filename || strEQ (filename, "-"))
- {
- file_offset stdin_pos;
-#if HAVE_SETMODE_DOS
- if (binary_transput)
- {
- if (isatty (STDIN_FILENO))
- fatal ("cannot read binary data from tty on this platform");
- setmode (STDIN_FILENO, O_BINARY);
- }
-#endif
- if (fstat (STDIN_FILENO, &st) != 0)
- pfatal ("fstat");
- if (S_ISREG (st.st_mode) && (stdin_pos = file_tell (stdin)) != -1)
- {
- pfp = stdin;
- file_pos = stdin_pos;
- }
- else
- {
- size_t charsread;
- int fd = make_tempfile (&TMPPATNAME, 'p', NULL, O_RDWR | O_BINARY, 0);
- TMPPATNAME_needs_removal = 1;
- pfp = fdopen (fd, "w+b");
- if (! pfp)
- pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME));
- for (st.st_size = 0;
- (charsread = fread (buf, 1, bufsize, stdin)) != 0;
- st.st_size += charsread)
- if (fwrite (buf, 1, charsread, pfp) != charsread)
- write_fatal ();
- if (ferror (stdin) || fclose (stdin) != 0)
- read_fatal ();
- if (fflush (pfp) != 0
- || file_seek (pfp, (file_offset) 0, SEEK_SET) != 0)
- write_fatal ();
- }
- }
+ pfp = stdin;
else
{
pfp = fopen (filename, binary_transput ? "rb" : "r");
if (!pfp)
pfatal ("Can't open patch file %s", quotearg (filename));
- if (fstat (fileno (pfp), &st) != 0)
- pfatal ("fstat");
+ }
+#if HAVE_SETMODE_DOS
+ if (binary_transput)
+ {
+ if (isatty (fileno (pfp)))
+ fatal ("cannot read binary data from tty on this platform");
+ setmode (fileno (pfp), O_BINARY);
+ }
+#endif
+ if (fstat (fileno (pfp), &st) != 0)
+ pfatal ("fstat");
+ if (S_ISREG (st.st_mode) && (pos = file_tell (pfp)) != -1)
+ file_pos = pos;
+ else
+ {
+ size_t charsread;
+ int fd = make_tempfile (&TMPPATNAME, 'p', NULL, O_RDWR | O_BINARY, 0);
+ FILE *read_pfp = pfp;
+ TMPPATNAME_needs_removal = 1;
+ pfp = fdopen (fd, "w+b");
+ if (! pfp)
+ pfatal ("Can't open stream for file %s", quotearg (TMPPATNAME));
+ for (st.st_size = 0;
+ (charsread = fread (buf, 1, bufsize, read_pfp)) != 0;
+ st.st_size += charsread)
+ if (fwrite (buf, 1, charsread, pfp) != charsread)
+ write_fatal ();
+ if (ferror (read_pfp) || fclose (read_pfp) != 0)
+ read_fatal ();
+ if (fflush (pfp) != 0
+ || file_seek (pfp, (file_offset) 0, SEEK_SET) != 0)
+ write_fatal ();
}
p_filesize = st.st_size;
if (p_filesize != (file_offset) p_filesize)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cca8a87..8559f24 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,6 +27,7 @@ TESTS = \
crlf-handling \
dash-o-append \
empty-files \
+ fifo \
file-modes \
filename-choice \
git-binary-diff \
diff --git a/tests/fifo b/tests/fifo
new file mode 100644
index 0000000..d6f0cc8
--- /dev/null
+++ b/tests/fifo
@@ -0,0 +1,42 @@
+# Copyright (C) 2009 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# in any medium, are permitted without royalty provided the copyright
+# notice and this notice are preserved.
+
+# Don't recognize hunks before a filename has been specified/seen
+
+. $srcdir/test-lib.sh
+
+require_cat
+use_local_patch
+use_tmpdir
+
+# ==============================================================
+
+cat > f.diff <<EOF
+--- /dev/null
++++ f
+@@ -0,0 +1 @@
++f
+EOF
+
+check 'patch --dry-run < f.diff' <<EOF
+patching file f
+EOF
+
+check 'patch --dry-run -i f.diff' <<EOF
+patching file f
+EOF
+
+mkfifo fifo
+
+cat f.diff > fifo &
+check 'patch --dry-run < fifo' <<EOF
+patching file f
+EOF
+
+cat f.diff > fifo &
+check 'patch --dry-run -i fifo' <<EOF
+patching file f
+EOF