]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
csplit: pacify GCC 13
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 27 Apr 2023 00:14:54 +0000 (17:14 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 27 Apr 2023 01:20:19 +0000 (18:20 -0700)
* 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

index 9a31697b619090d524540fe3df1981cf90ab7053..09e1468c247d4619efaa47e8681fbe282f641ff2 100644 (file)
@@ -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. */