diff options
author | Jim Meyering <jim@meyering.net> | 2003-11-16 12:25:35 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2003-11-16 12:25:35 +0000 |
commit | 51ab3265f8444eeb09faf47c9d5d4a1a8b036025 (patch) | |
tree | dfbad0fb1119aab39ac73cf025204f8641a24fad /src | |
parent | 8560d7254ba1cbf29f5b6941e65c1619c01b33a6 (diff) | |
download | coreutils-51ab3265f8444eeb09faf47c9d5d4a1a8b036025.tar.xz |
Fix read-from-free'd-buffer error detected by valgrind.
(remove_line): Don't return a pointer to data in
a freed buffer. Instead, arrange to free the buffer on the
subsequent call.
Diffstat (limited to 'src')
-rw-r--r-- | src/csplit.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/csplit.c b/src/csplit.c index d2294b90e..dca288408 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -526,9 +526,20 @@ get_first_line_in_buffer (void) static struct cstring * remove_line (void) { + /* If non-NULL, this is the buffer for which the previous call + returned the final line. So now, presuming that line has been + processed, we can free the buffer and reset this pointer. */ + static struct buffer_record *prev_buf = NULL; + struct cstring *line; /* Return value. */ struct line *l; /* For convenience. */ + if (prev_buf) + { + free_buffer (prev_buf); + prev_buf = NULL; + } + if (head == NULL && !load_buffer ()) return NULL; @@ -548,10 +559,11 @@ remove_line (void) head->curr_line = l->next; if (head->curr_line == NULL || head->curr_line->used == 0) { - /* Go on to the next data block. */ - struct buffer_record *b = head; + /* Go on to the next data block. + but first record the current one so we can free it + once the line we're returning has been processed. */ + prev_buf = head; head = head->next; - free_buffer (b); } } |