]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(pipe_lines): Add variable `nbytes' so we can free
authorJim Meyering <jim@meyering.net>
Sun, 6 Aug 2000 08:44:11 +0000 (08:44 +0000)
committerJim Meyering <jim@meyering.net>
Sun, 6 Aug 2000 08:44:11 +0000 (08:44 +0000)
`tmp' immediately after read loop.  Don't process an empty file.
This fixes a buffer-underrun error -- also thanks to bounded pointers.

src/tail.c

index 61f13a4d40592e533dace3f618974a2b5b7b6bfd..79be4f3cb0d3c42a70b4aa08fa317faa9499e920 100644 (file)
@@ -466,6 +466,7 @@ pipe_lines (const char *pretty_filename, int fd, long int n_lines)
   int i;                       /* Index into buffers.  */
   int total_lines = 0;         /* Total number of newlines in all buffers.  */
   int errors = 0;
+  int nbytes;                  /* Size of most recent read */
 
   first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
   first->nbytes = first->nlines = 0;
@@ -473,7 +474,7 @@ pipe_lines (const char *pretty_filename, int fd, long int n_lines)
   tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
 
   /* Input is always read into a fresh buffer.  */
-  while ((tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
+  while ((nbytes = tmp->nbytes = safe_read (fd, tmp->buffer, BUFSIZ)) > 0)
     {
       tmp->nlines = 0;
       tmp->next = NULL;
@@ -511,15 +512,19 @@ pipe_lines (const char *pretty_filename, int fd, long int n_lines)
            tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
        }
     }
-  if (tmp->nbytes == -1)
+
+  free ((char *) tmp);
+
+  if (nbytes < 0)
     {
       error (0, errno, "%s", pretty_filename);
       errors = 1;
-      free ((char *) tmp);
       goto free_lbuffers;
     }
 
-  free ((char *) tmp);
+  /* If the file is empty, then bail out.  */
+  if (nbytes + last->nbytes == 0)
+    goto free_lbuffers;
 
   /* This prevents a core dump when the pipe contains no newlines.  */
   if (n_lines == 0)