(pipe_lines): Add variable `nbytes' so we can free

`tmp' immediately after read loop.  Don't process an empty file.
This fixes a buffer-underrun error -- also thanks to bounded pointers.
This commit is contained in:
Jim Meyering committed 2000-08-06 08:44:11 +00:00
1 parent 773a575cd5
commit 95b686e751
1 file changed
+9 -4
+9 -4
View 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)