]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
sort: prefer xpalloc to x2nrealloc
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 7 Nov 2024 22:25:17 +0000 (14:25 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 9 Nov 2024 07:41:18 +0000 (23:41 -0800)
* src/sort.c (struct buffer, temp_dir_count, temp_dir_alloc)
(create_temp_file, add_temp_dir, fillbuf):
Prefer idx_t to ptrdiff_t/size_t for nonnegative directory counts.
(add_temp_dir, fillbuf): Use xpalloc, not x2nrealloc.
* src/system.h (X2NREALLOC): Remove; no longer used.

src/sort.c
src/system.h

index 4230d32d8414fdde0013883239f63c9ce7926e9b..7205007745041d27e9b501b669e4e543f4da9751 100644 (file)
@@ -190,7 +190,7 @@ struct buffer
                                    - an array of lines, in reverse order.  */
   size_t used;                 /* Number of bytes used for input data.  */
   size_t nlines;               /* Number of lines in the line array.  */
-  size_t alloc;                        /* Number of bytes allocated. */
+  idx_t alloc;                 /* Number of bytes allocated. */
   size_t left;                 /* Number of bytes left from previous reads. */
   size_t line_bytes;           /* Number of bytes to reserve for each line. */
   bool eof;                    /* An EOF has been read.  */
@@ -325,10 +325,10 @@ static size_t sort_size;
 static char const **temp_dirs;
 
 /* Number of temporary directory names used.  */
-static size_t temp_dir_count;
+static idx_t temp_dir_count;
 
 /* Number of allocated slots in temp_dirs.  */
-static size_t temp_dir_alloc;
+static idx_t temp_dir_alloc;
 
 /* Flag to reverse the order of all comparisons. */
 static bool reverse;
@@ -846,7 +846,7 @@ static struct tempnode *
 create_temp_file (int *pfd, bool survive_fd_exhaustion)
 {
   static char const slashbase[] = "/sortXXXXXX";
-  static size_t temp_dir_index;
+  static idx_t temp_dir_index;
   int fd;
   int saved_errno;
   char const *temp_dir = temp_dirs[temp_dir_index];
@@ -1235,7 +1235,7 @@ static void
 add_temp_dir (char const *dir)
 {
   if (temp_dir_count == temp_dir_alloc)
-    temp_dirs = X2NREALLOC (temp_dirs, &temp_dir_alloc);
+    temp_dirs = xpalloc (temp_dirs, &temp_dir_alloc, 1, -1, sizeof *temp_dirs);
 
   temp_dirs[temp_dir_count++] = dir;
 }
@@ -1865,8 +1865,8 @@ fillbuf (struct buffer *buf, FILE *fp, char const *file)
         /* The current input line is too long to fit in the buffer.
            Increase the buffer size and try again, keeping it properly
            aligned.  */
-        size_t line_alloc = buf->alloc / sizeof (struct line);
-        buf->buf = x2nrealloc (buf->buf, &line_alloc, sizeof (struct line));
+        idx_t line_alloc = buf->alloc / sizeof (struct line);
+        buf->buf = xpalloc (buf->buf, &line_alloc, 1, -1, sizeof (struct line));
         buf->alloc = line_alloc * sizeof (struct line);
       }
     }
index fc038a9797995c484588b1d5d3e0d1ff56a96ce7..fb5bd51cd85695ddecb759cb0938e325e27bcf6f 100644 (file)
@@ -237,13 +237,6 @@ uid_t getuid (void);
 #include "xalloc.h"
 #include "verify.h"
 
-/* This is simply a shorthand for the common case in which
-   the third argument to x2nrealloc would be 'sizeof *(P)'.
-   Ensure that sizeof *(P) is *not* 1.  In that case, it'd be
-   better to use X2REALLOC, although not strictly necessary.  */
-#define X2NREALLOC(P, PN) verify_expr (sizeof *(P) != 1, \
-                                       x2nrealloc (P, PN, sizeof *(P)))
-
 /* 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.