]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
pr: prefer xpalloc to x2realloc
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 8 Nov 2024 00:39:35 +0000 (16:39 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 Nov 2024 07:41:18 +0000 (23:41 -0800)
* src/pr.c (buff_allocated, main):
Prefer idx_t to size_t for sizes.
(main, store_char): Use xpalloc, not x2realloc.
(init_store_cols): Check for multiplication overflow ourselves
and use ximalloc, not xnmalloc.  This is a bit simpler.
* src/system.h (X2REALLOC): Remove; no longer used.

src/pr.c
src/system.h

index 88a330bc0c34961298b8ebb04b5c29102c40b6e1..a08da2fd75c6bc867c8e82b1c1d7a080df720121 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
@@ -460,7 +460,7 @@ static unsigned int buff_current;
 
 /* The number of characters in buff.
    Used for allocation of buff and to detect overflow of buff. */
-static size_t buff_allocated;
+static idx_t buff_allocated;
 
 /* Array of indices into buff.
    Each entry is an index of the first character of a line.
@@ -864,8 +864,8 @@ main (int argc, char **argv)
 
   /* Accumulate the digits of old-style options like -99.  */
   char *column_count_string = nullptr;
-  size_t n_digits = 0;
-  size_t n_alloc = 0;
+  idx_t n_digits = 0;
+  idx_t n_alloc = 0;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -891,8 +891,8 @@ main (int argc, char **argv)
         {
           /* Accumulate column-count digits specified via old-style options. */
           if (n_digits + 1 >= n_alloc)
-            column_count_string
-              = X2REALLOC (column_count_string, &n_alloc);
+            column_count_string = xpalloc (column_count_string, &n_alloc, 2, -1,
+                                           sizeof *column_count_string);
           column_count_string[n_digits++] = c;
           column_count_string[n_digits] = '\0';
           continue;
@@ -1910,11 +1910,13 @@ print_page (void)
 static void
 init_store_cols (void)
 {
+  /* Tune this.  */
   int total_lines, total_lines_1, chars_per_column_1, chars_if_truncate;
   if (ckd_mul (&total_lines, lines_per_body, columns)
       || ckd_add (&total_lines_1, total_lines, 1)
       || ckd_add (&chars_per_column_1, chars_per_column, 1)
-      || ckd_mul (&chars_if_truncate, total_lines, chars_per_column_1))
+      || ckd_mul (&chars_if_truncate, total_lines, chars_per_column_1)
+      || ckd_mul (&buff_allocated, chars_if_truncate, use_col_separator + 1))
     integer_overflow ();
 
   free (line_vector);
@@ -1925,9 +1927,7 @@ init_store_cols (void)
   end_vector = xnmalloc (total_lines, sizeof *end_vector);
 
   free (buff);
-  buff = xnmalloc (chars_if_truncate, use_col_separator + 1);
-  buff_allocated = chars_if_truncate;  /* Tune this. */
-  buff_allocated *= use_col_separator + 1;
+  buff = ximalloc (buff_allocated);
 }
 
 /* Store all but the rightmost column.
@@ -2021,7 +2021,7 @@ store_char (char c)
   if (buff_current >= buff_allocated)
     {
       /* May be too generous. */
-      buff = X2REALLOC (buff, &buff_allocated);
+      buff = xpalloc (buff, &buff_allocated, 1, -1, sizeof *buff);
     }
   buff[buff_current++] = c;
 }
index fb5bd51cd85695ddecb759cb0938e325e27bcf6f..ea30fb4b6ac59d7650b3ae078d7fb15c4f27422a 100644 (file)
@@ -236,14 +236,6 @@ uid_t getuid (void);
 #include "idx.h"
 #include "xalloc.h"
 #include "verify.h"
-
-/* Using x2realloc (when appropriate) usually makes your code more
-   readable than using x2nrealloc, but it also makes it so your
-   code will malfunction if sizeof *(P) ever becomes 2 or greater.
-   So use this macro instead of using x2realloc directly.  */
-#define X2REALLOC(P, PN) verify_expr (sizeof *(P) == 1, \
-                                      x2realloc (P, PN))
-
 #include "unlocked-io.h"
 #include "same-inode.h"