]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
expand: prefer xpalloc to x2nrealloc
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Nov 2024 00:31:18 +0000 (16:31 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 Nov 2024 07:41:18 +0000 (23:41 -0800)
* src/expand-common.c (max_column_width, n_tabs_allocated)
(first_free_tab, add_tab_stop, parse_tab_stops, validate_tab_stops)
(get_next_tab_column):
Use idx_t for sizes.  All uses changed.
(add_tab_stop): Use xpalloc instead of X2NREALLOC.
Use ckd_add to check for overflow, instead of doing it by hand.

src/expand-common.c
src/expand-common.h
src/expand.c
src/unexpand.c

index c95998dc690cb4b630a8df0c02cb7bdf619dc4ef..ba14c6d81a7139901c67e194e33489a21b891ae2 100644 (file)
@@ -39,7 +39,7 @@ static uintmax_t extend_size = 0;
 static uintmax_t increment_size = 0;
 
 /* The maximum distance between tab stops.  */
-size_t max_column_width;
+idx_t max_column_width;
 
 /* Array of the explicit column numbers of the tab stops;
    after 'tab_list' is exhausted, each additional tab is replaced
@@ -47,11 +47,11 @@ size_t max_column_width;
 static uintmax_t *tab_list = nullptr;
 
 /* The number of allocated entries in 'tab_list'.  */
-static size_t n_tabs_allocated = 0;
+static idx_t n_tabs_allocated = 0;
 
 /* The index of the first invalid element of 'tab_list',
    where the next element can be added.  */
-static size_t first_free_tab = 0;
+static idx_t first_free_tab = 0;
 
 /* Null-terminated array of input filenames.  */
 static char **file_list = nullptr;
@@ -78,14 +78,13 @@ add_tab_stop (uintmax_t tabval)
   uintmax_t column_width = prev_column <= tabval ? tabval - prev_column : 0;
 
   if (first_free_tab == n_tabs_allocated)
-    tab_list = X2NREALLOC (tab_list, &n_tabs_allocated);
+    tab_list = xpalloc (tab_list, &n_tabs_allocated, 1, -1, sizeof *tab_list);
   tab_list[first_free_tab++] = tabval;
 
   if (max_column_width < column_width)
     {
-      if (SIZE_MAX < column_width)
+      if (ckd_add (&max_column_width, column_width, 0))
         error (EXIT_FAILURE, 0, _("tabs are too far apart"));
-      max_column_width = column_width;
     }
 }
 
@@ -196,7 +195,7 @@ parse_tab_stops (char const *stops)
           /* Detect overflow.  */
           if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0'))
             {
-              size_t len = strspn (num_start, "0123456789");
+              idx_t len = strspn (num_start, "0123456789");
               char *bad_num = ximemdup0 (num_start, len);
               error (0, 0, _("tab stop is too large %s"), quote (bad_num));
               free (bad_num);
@@ -231,11 +230,11 @@ parse_tab_stops (char const *stops)
    contains only nonzero, ascending values.  */
 
 static void
-validate_tab_stops (uintmax_t const *tabs, size_t entries)
+validate_tab_stops (uintmax_t const *tabs, idx_t entries)
 {
   uintmax_t prev_tab = 0;
 
-  for (size_t i = 0; i < entries; i++)
+  for (idx_t i = 0; i < entries; i++)
     {
       if (tabs[i] == 0)
         error (EXIT_FAILURE, 0, _("tab size cannot be 0"));
@@ -273,7 +272,7 @@ finalize_tab_stops (void)
 
 
 extern uintmax_t
-get_next_tab_column (const uintmax_t column, size_t *tab_index,
+get_next_tab_column (const uintmax_t column, idx_t *tab_index,
                      bool *last_tab)
 {
   *last_tab = false;
index 1a57108e428d7bdf799c679c86bcf0978d94d6ac..9234691d62aaad8ca8e23fcd1a72480049f48f09 100644 (file)
@@ -20,7 +20,7 @@
 extern bool convert_entire_line;
 
 /* The maximum distance between tab stops.  */
-extern size_t max_column_width;
+extern idx_t max_column_width;
 
 /* The desired exit status.  */
 extern int exit_status;
@@ -36,7 +36,7 @@ parse_tab_stops (char const *stops) _GL_ATTRIBUTE_NONNULL ();
 
 /* TODO: Document */
 extern uintmax_t
-get_next_tab_column (const uintmax_t column, size_t *tab_index,
+get_next_tab_column (const uintmax_t column, idx_t *tab_index,
                      bool *last_tab)
   _GL_ATTRIBUTE_NONNULL ((3));
 
index a6176a9743179e3d00f4034766574bce67d48cf4..419cf19dff7b5dcf4d0b5baba85168ba44fb3ee4 100644 (file)
@@ -116,7 +116,7 @@ expand (void)
       uintmax_t column = 0;
 
       /* Index in TAB_LIST of next tab stop to examine.  */
-      size_t tab_index = 0;
+      idx_t tab_index = 0;
 
 
       /* Convert a line of text.  */
index aca67dd7389f3b3d28439455afdea28fc101bb40..846f8467e9461ab86997591ca7dc51415752b3b3 100644 (file)
@@ -117,7 +117,7 @@ unexpand (void)
   /* The worst case is a non-blank character, then one blank, then a
      tab stop, then MAX_COLUMN_WIDTH - 1 blanks, then a non-blank; so
      allocate MAX_COLUMN_WIDTH bytes to store the blanks.  */
-  pending_blank = xmalloc (max_column_width);
+  pending_blank = ximalloc (max_column_width);
 
   while (true)
     {
@@ -138,7 +138,7 @@ unexpand (void)
       uintmax_t next_tab_column = 0;
 
       /* Index in TAB_LIST of next tab stop to examine.  */
-      size_t tab_index = 0;
+      idx_t tab_index = 0;
 
       /* If true, the first pending blank came just before a tab stop.  */
       bool one_blank_before_tab_stop = false;
@@ -149,7 +149,7 @@ unexpand (void)
       bool prev_blank = true;
 
       /* Number of pending columns of blanks.  */
-      size_t pending = 0;
+      idx_t pending = 0;
 
 
       /* Convert a line of text.  */