summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2023-04-26 17:14:54 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2023-04-26 18:20:19 -0700
commitd178b497542b19571dd6896db561c46ec2e451c3 (patch)
treea7900156101189d20d33e38e7432964e5df4634c
parent941027eeb77e3cfa2923f877d5ba45d7d70a39a5 (diff)
downloadcoreutils-d178b497542b19571dd6896db561c46ec2e451c3.tar.gz
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.
-rw-r--r--src/csplit.c41
1 files 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. */