Wolfram Kleff reported that head may read too much which could cause
errors when the file operated on is special:

http://bugs.debian.org/113183

The following patch based on the one given by Wolfram makes sure that head
does not read more than what is precribed.
-- 
Debian GNU/Linux 2.2 is out! ( http://www.debian.org/ )
Email:  Herbert Xu ~{PmV>HI~} <[EMAIL PROTECTED]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Index: src/head.c
===================================================================
RCS file: /home/gondolin/herbert/src/CVS/debian/textutils/src/head.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 head.c
--- src/head.c  22 Apr 1999 05:25:03 -0000      1.1.1.1
+++ src/head.c  22 Sep 2001 22:29:16 -0000
@@ -45,6 +45,10 @@
 /* Size of atomic reads. */
 #define BUFSIZE (512 * 8)
 
+/* Undefine, to avoid warning about redefinition on some systems.  */
+#undef min
+#define min(x, y) ((x) < (y) ? (x) : (y))
+
 /* If nonzero, print filename headers. */
 static int print_headers;
 
@@ -125,7 +129,7 @@
 
   while (bytes_to_write)
     {
-      bytes_read = safe_read (fd, buffer, BUFSIZE);
+      bytes_read = safe_read (fd, buffer, min(bytes_to_write, BUFSIZE));
       if (bytes_read < 0)
        {
          error (0, errno, "%s", filename);
@@ -133,8 +137,6 @@
        }
       if (bytes_read == 0)
        break;
-      if (bytes_read > bytes_to_write)
-       bytes_read = bytes_to_write;
       if (fwrite (buffer, 1, bytes_read, stdout) == 0)
        error (EXIT_FAILURE, errno, _("write error"));
       bytes_to_write -= bytes_read;

Reply via email to