From d178b497542b19571dd6896db561c46ec2e451c3 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 26 Apr 2023 17:14:54 -0700 Subject: csplit: pacify GCC 13 * src/csplit.c (load_buffer): Refactor for clarity. This also xpacifies gcc -Wanalyzer-use-of-uninitialized-value. When reallocating the buffer, grow it by a factor of 1.5, not 2. --- src/csplit.c | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/csplit.c b/src/csplit.c index 9a31697b6..09e1468c2 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -451,32 +451,25 @@ save_buffer (struct buffer_record *buf) static bool load_buffer (void) { - struct buffer_record *b; - idx_t bytes_wanted = START_SIZE; /* Minimum buffer size. */ - idx_t bytes_avail; /* Size of new buffer created. */ - idx_t lines_found; /* Number of lines in this new buffer. */ - char *p; /* Place to load into buffer. */ - if (have_read_eof) return false; /* We must make the buffer at least as large as the amount of data in the partial line left over from the last call, plus room for a sentinel '\n'. */ - if (bytes_wanted <= hold_count) - bytes_wanted = hold_count + 1; + idx_t bytes_wanted = MAX (START_SIZE, hold_count + 1); while (true) { - b = get_new_buffer (bytes_wanted); - bytes_avail = b->bytes_alloc; /* Size of buffer returned. */ - p = b->buffer; + struct buffer_record *b = get_new_buffer (bytes_wanted); + idx_t bytes_alloc = b->bytes_alloc; + idx_t bytes_avail = bytes_alloc; + char *p = b->buffer; /* First check the 'holding' area for a partial line. */ if (hold_count) { - memcpy (p, hold_area, hold_count); - p += hold_count; + p = mempcpy (p, hold_area, hold_count); b->bytes_used += hold_count; bytes_avail -= hold_count; hold_count = 0; @@ -484,22 +477,18 @@ load_buffer (void) b->bytes_used += read_input (p, bytes_avail - 1); - lines_found = record_line_starts (b); - - if (lines_found || have_read_eof) - break; + if (record_line_starts (b) != 0) + { + save_buffer (b); + return true; + } - if (INT_MULTIPLY_WRAPV (b->bytes_alloc, 2, &bytes_wanted)) - xalloc_die (); free_buffer (b); + if (have_read_eof) + return false; + if (INT_ADD_WRAPV (bytes_alloc, bytes_alloc >> 1, &bytes_wanted)) + xalloc_die (); } - - if (lines_found) - save_buffer (b); - else - free_buffer (b); - - return lines_found != 0; } /* Return the line number of the first line that has not yet been retrieved. */ -- cgit v1.2.1