]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer ckd_add to INT_ADD_WRAPV etc
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Jul 2023 18:31:41 +0000 (11:31 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Jul 2023 18:51:16 +0000 (11:51 -0700)
* bootstrap.conf (gnulib_modules): Add stdckdint.
Also, in C source code, prefer C23 macros like ckd_add
to their Gnulib near-equivalents like INT_ADD_WRAPV.
Include <stdckdint.h> as needed.

13 files changed:
bootstrap.conf
src/cat.c
src/copy.c
src/csplit.c
src/dd.c
src/kill.c
src/nl.c
src/pinky.c
src/pr.c
src/split.c
src/truncate.c
src/wc.c
src/who.c

index dbf253b645526c7e674dabd99c5bb2b74510bb29..aaec1fb03a62e8955f2f8081c5244bc8e8691297 100644 (file)
@@ -253,6 +253,7 @@ gnulib_modules="
   stat-size
   stat-time
   stdbool
+  stdckdint
   stdlib-safer
   stpcpy
   stpncpy
index 779bcc4dc019287e1687bd9264a8b9026f16b4f3..880fcf2e7fe882e1b107306e9fad3b3bc70511bd 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -23,6 +23,7 @@
 
 #include <config.h>
 
+#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -766,9 +767,9 @@ main (int argc, char **argv)
              on some paging implementations.  */
 
           idx_t bufsize;
-          if (INT_MULTIPLY_WRAPV (insize, 4, &bufsize)
-              || INT_ADD_WRAPV (bufsize, outsize, &bufsize)
-              || INT_ADD_WRAPV (bufsize, LINE_COUNTER_BUF_LEN - 1, &bufsize))
+          if (ckd_mul (&bufsize, insize, 4)
+              || ckd_add (&bufsize, bufsize, outsize)
+              || ckd_add (&bufsize, bufsize, LINE_COUNTER_BUF_LEN - 1))
             xalloc_die ();
           char *outbuf = xalignalloc (page_size, bufsize);
 
index c286228441de945580b485da70e5d4b8dc8225b2..ddc9eab302c4aca8b5e3d5d85d8d22c07c17f044 100644 (file)
@@ -17,6 +17,7 @@
 /* Extracted from cp.c and librarified by Jim Meyering.  */
 
 #include <config.h>
+#include <stdckdint.h>
 #include <stdio.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -458,7 +459,7 @@ sparse_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
             }
           else  /* Coalesce writes/seeks.  */
             {
-              if (INT_ADD_WRAPV (psize, csize, &psize))
+              if (ckd_add (&psize, psize, csize))
                 {
                   error (0, 0, _("overflow reading %s"), quoteaf (src_name));
                   return false;
index a11298404f4ae7bc2a4d488e7feed60865dbf1d8..4052ca9089ed862722a08262a2accd67c755e981 100644 (file)
@@ -22,6 +22,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include <signal.h>
+#include <stdckdint.h>
 
 #include "system.h"
 
@@ -490,7 +491,7 @@ load_buffer (void)
       free_buffer (b);
       if (have_read_eof)
         return false;
-      if (INT_ADD_WRAPV (bytes_alloc, bytes_alloc >> 1, &bytes_wanted))
+      if (ckd_add (&bytes_wanted, bytes_alloc, bytes_alloc >> 1))
         xalloc_die ();
     }
 }
@@ -1370,7 +1371,7 @@ main (int argc, char **argv)
        ? max_out (suffix)
        : MAX (INT_STRLEN_BOUND (int), digits));
   idx_t filename_size;
-  if (INT_ADD_WRAPV (prefix_len, max_digit_string_len + 1, &filename_size))
+  if (ckd_add (&filename_size, prefix_len, max_digit_string_len + 1))
     xalloc_die ();
   filename_space = ximalloc (filename_size);
 
index 8df93d521df435c5791af2af6ab5ec777142bb6d..21f7a4edea6ec6e371d1b08334f4e296f38d6330 100644 (file)
--- a/src/dd.c
+++ b/src/dd.c
@@ -20,6 +20,7 @@
 
 #include <sys/types.h>
 #include <signal.h>
+#include <stdckdint.h>
 
 #include "system.h"
 #include "alignalloc.h"
@@ -1015,7 +1016,7 @@ cache_round (int fd, off_t len)
   if (len)
     {
       intmax_t c_pending;
-      if (INT_ADD_WRAPV (*pending, len, &c_pending))
+      if (ckd_add (&c_pending, *pending, len))
         c_pending = INTMAX_MAX;
       *pending = c_pending % IO_BUFSIZE;
       if (c_pending > *pending)
@@ -1445,7 +1446,7 @@ parse_integer (char const *str, strtol_error *invalid)
           e = f;
           result = indeterminate;
         }
-      else if (INT_MULTIPLY_WRAPV (n, o, &result)
+      else if (ckd_mul (&result, n, o)
                || (result != 0 && ((e | f) & LONGINT_OVERFLOW)))
         {
           e = LONGINT_OVERFLOW;
@@ -1780,7 +1781,7 @@ swab_buffer (char *buf, idx_t *nread, int *saved_byte)
 static void
 advance_input_offset (intmax_t offset)
 {
-  if (0 <= input_offset && INT_ADD_WRAPV (input_offset, offset, &input_offset))
+  if (0 <= input_offset && ckd_add (&input_offset, input_offset, offset))
     input_offset = -1;
 }
 
@@ -1803,8 +1804,8 @@ skip (int fdesc, char const *file, intmax_t records, idx_t blocksize,
 
   errno = 0;
   off_t offset;
-  if (! INT_MULTIPLY_WRAPV (records, blocksize, &offset)
-      && ! INT_ADD_WRAPV (offset, *bytes, &offset)
+  if (! ckd_mul (&offset, records, blocksize)
+      && ! ckd_add (&offset, offset, *bytes)
       && 0 <= lseek (fdesc, offset, SEEK_CUR))
     {
       if (fdesc == STDIN_FILENO)
@@ -2111,8 +2112,8 @@ dd_copy (void)
     {
       intmax_t us_bytes;
       bool us_bytes_overflow =
-        (INT_MULTIPLY_WRAPV (skip_records, input_blocksize, &us_bytes)
-         || INT_ADD_WRAPV (skip_bytes, us_bytes, &us_bytes));
+        (ckd_mul (&us_bytes, skip_records, input_blocksize)
+         || ckd_add (&us_bytes, skip_bytes, us_bytes));
       off_t input_offset0 = input_offset;
       intmax_t us_blocks = skip (STDIN_FILENO, input_file,
                                  skip_records, input_blocksize, &skip_bytes);
@@ -2477,8 +2478,8 @@ main (int argc, char **argv)
            | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
 
       off_t size;
-      if ((INT_MULTIPLY_WRAPV (seek_records, output_blocksize, &size)
-           || INT_ADD_WRAPV (seek_bytes, size, &size))
+      if ((ckd_mul (&size, seek_records, output_blocksize)
+           || ckd_add (&size, seek_bytes, size))
           && !(conversions_mask & C_NOTRUNC))
         error (EXIT_FAILURE, 0,
                _("offset too large: "
index ba448d995c55fecaade59673e455018dd949aac5..81f6fba8b880c480a02fae3002c176110d0ff2d9 100644 (file)
@@ -17,6 +17,7 @@
 /* Written by Paul Eggert.  */
 
 #include <config.h>
+#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -200,7 +201,7 @@ send_signals (int signum, char *const *argv)
       intmax_t n = (errno = 0, strtoimax (arg, &endp, 10));
       pid_t pid;
 
-      if (errno == ERANGE || INT_ADD_WRAPV (n, 0, &pid)
+      if (errno == ERANGE || ckd_add (&pid, n, 0)
           || arg == endp || *endp)
         {
           error (0, 0, _("%s: invalid process id"), quote (arg));
index 7b9b369807bb5fe0de06878d66961624dd2167f5..e41e695e7b5019082827dd8cb5e7c87ffd43f7d0 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -19,6 +19,7 @@
 
 #include <config.h>
 
+#include <stdckdint.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <getopt.h>
@@ -283,7 +284,7 @@ print_lineno (void)
 
   printf (lineno_format, lineno_width, line_no, separator_str);
 
-  if (INT_ADD_WRAPV (line_no, page_incr, &line_no))
+  if (ckd_add (&line_no, line_no, page_incr))
     line_no_overflow = true;
 }
 
index e3e19968868f8e1d44b7cd5314a887b082a7b218..76da073f660d02c8f04a9c82efa75bad551eb7ad 100644 (file)
@@ -19,6 +19,7 @@
 #include <config.h>
 #include <getopt.h>
 #include <pwd.h>
+#include <stdckdint.h>
 #include <stdio.h>
 
 #include <sys/types.h>
@@ -110,8 +111,8 @@ create_fullname (char const *gecos_name, char const *user_name)
     {
       size_t ulen = strlen (user_name);
       size_t product;
-      if (INT_MULTIPLY_WRAPV (ulen, ampersands - 1, &product)
-          || INT_ADD_WRAPV (rsize, product, &rsize))
+      if (ckd_mul (&product, ulen, ampersands - 1)
+          || ckd_add (&rsize, rsize, product))
         xalloc_die ();
     }
 
index 92072aba169bb58b9c340c8533ba282ed735f4ae..59159a308e050eddc30ea7818d1182c2098f08ae 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
 #include <config.h>
 
 #include <getopt.h>
+#include <stdckdint.h>
 #include <sys/types.h>
 #include "system.h"
 #include "fadvise.h"
@@ -1284,10 +1285,10 @@ init_parameters (int number_of_files)
     }
 
   int sep_chars, useful_chars;
-  if (INT_MULTIPLY_WRAPV (columns - 1, col_sep_length, &sep_chars))
+  if (ckd_mul (&sep_chars, columns - 1, col_sep_length))
     sep_chars = INT_MAX;
-  if (INT_SUBTRACT_WRAPV (chars_per_line - chars_used_by_number, sep_chars,
-                          &useful_chars))
+  if (ckd_sub (&useful_chars, chars_per_line - chars_used_by_number,
+               sep_chars))
     useful_chars = 0;
   chars_per_column = useful_chars / columns;
 
@@ -1908,11 +1909,10 @@ static void
 init_store_cols (void)
 {
   int total_lines, total_lines_1, chars_per_column_1, chars_if_truncate;
-  if (INT_MULTIPLY_WRAPV (lines_per_body, columns, &total_lines)
-      || INT_ADD_WRAPV (total_lines, 1, &total_lines_1)
-      || INT_ADD_WRAPV (chars_per_column, 1, &chars_per_column_1)
-      || INT_MULTIPLY_WRAPV (total_lines, 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))
     integer_overflow ();
 
   free (line_vector);
index f4514d4d28f842fc65c97611dd22cdb497fd79e6..a3ab8d88b7463842ec715ac3488f0f2a662130a4 100644 (file)
@@ -21,6 +21,7 @@
    * support --suppress-matched as in csplit.  */
 #include <config.h>
 
+#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <signal.h>
@@ -188,7 +189,7 @@ set_suffix_length (intmax_t n_units, enum Split_type split_type)
                  incrementing a suffix size arbitrarily,
                  as that would break sort order for files
                  generated from multiple split runs.  */
-              if (INT_ADD_WRAPV (n_units_end, n_start, &n_units_end))
+              if (ckd_add (&n_units_end, n_units_end, n_start))
                 n_units_end = INTMAX_MAX;
             }
 
@@ -288,7 +289,7 @@ copy_to_tmpfile (int fd, char *buf, idx_t bufsize)
     {
       if (fwrite (buf, 1, r, tmp) != r)
         return -1;
-      if (INT_ADD_WRAPV (copied, r, &copied))
+      if (ckd_add (&copied, copied, r))
         {
           errno = EOVERFLOW;
           return -1;
@@ -338,7 +339,7 @@ input_file_size (int fd, struct stat const *st, char *buf, idx_t bufsize)
     }
 
   if (end == OFF_T_MAX /* E.g., /dev/zero on GNU/Hurd.  */
-      || (cur < end && INT_ADD_WRAPV (size, end - cur, &size)))
+      || (cur < end && ckd_add (&size, size, end - cur)))
     {
       errno = EOVERFLOW;
       return -1;
@@ -379,8 +380,8 @@ new_name:
 
           outbase_length = strlen (outbase);
           addsuf_length = additional_suffix ? strlen (additional_suffix) : 0;
-          overflow = INT_ADD_WRAPV (outbase_length + addsuf_length,
-                                    suffix_length, &outfile_length);
+          overflow = ckd_add (&outfile_length, outbase_length + addsuf_length,
+                              suffix_length);
         }
       else
         {
@@ -389,12 +390,12 @@ new_name:
              the generated suffix into the prefix (base), and
              reinitializing the now one longer suffix.  */
 
-          overflow = INT_ADD_WRAPV (outfile_length, 2, &outfile_length);
+          overflow = ckd_add (&outfile_length, outfile_length, 2);
           suffix_length++;
         }
 
       idx_t outfile_size;
-      overflow |= INT_ADD_WRAPV (outfile_length, 1, &outfile_size);
+      overflow |= ckd_add (&outfile_size, outfile_length, 1);
       if (overflow)
         xalloc_die ();
       outfile = xirealloc (outfile, outfile_size);
@@ -1500,8 +1501,8 @@ main (int argc, char **argv)
           if (digits_optind != 0 && digits_optind != this_optind)
             n_units = 0;       /* More than one number given; ignore other. */
           digits_optind = this_optind;
-          if (INT_MULTIPLY_WRAPV (n_units, 10, &n_units)
-              || INT_ADD_WRAPV (n_units, c - '0', &n_units))
+          if (ckd_mul (&n_units, n_units, 10)
+              || ckd_add (&n_units, n_units, c - '0'))
             n_units = INTMAX_MAX;
           break;
 
index 967da65edc279a15cbd917bdc70f4344eff1d7d6..7ecd595d8619fb2fe6f31bab79b80069af9754a8 100644 (file)
@@ -21,6 +21,7 @@
    to better fit the "GNU" environment.  */
 
 #include <config.h>             /* sets _FILE_OFFSET_BITS=64 etc. */
+#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -116,7 +117,7 @@ do_ftruncate (int fd, char const *fname, off_t ssize, off_t rsize,
     {
       ptrdiff_t blksize = ST_BLKSIZE (sb);
       intmax_t ssize0 = ssize;
-      if (INT_MULTIPLY_WRAPV (ssize, blksize, &ssize))
+      if (ckd_mul (&ssize, ssize, blksize))
         {
           error (0, 0,
                  _("overflow in %" PRIdMAX
@@ -172,7 +173,7 @@ do_ftruncate (int fd, char const *fname, off_t ssize, off_t rsize,
               off_t r = fsize % ssize;
               ssize = r == 0 ? 0 : ssize - r;
             }
-          if (INT_ADD_WRAPV (fsize, ssize, &nsize))
+          if (ckd_add (&nsize, fsize, ssize))
             {
               error (0, 0, _("overflow extending size of file %s"),
                      quoteaf (fname));
index 1330734d51c7ccf00d584d43200671bab3867acd..b1d82074d36fc2037e4390a6f81cf9fd0a420e59 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -19,6 +19,7 @@
 
 #include <config.h>
 
+#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
@@ -666,13 +667,13 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos)
   if (total_mode != total_only)
     write_counts (lines, words, chars, bytes, linelength, file_x);
 
-  if (INT_ADD_WRAPV (total_lines, lines, &total_lines))
+  if (ckd_add (&total_lines, total_lines, lines))
     total_lines_overflow = true;
-  if (INT_ADD_WRAPV (total_words, words, &total_words))
+  if (ckd_add (&total_words, total_words, words))
     total_words_overflow = true;
-  if (INT_ADD_WRAPV (total_chars, chars, &total_chars))
+  if (ckd_add (&total_chars, total_chars, chars))
     total_chars_overflow = true;
-  if (INT_ADD_WRAPV (total_bytes, bytes, &total_bytes))
+  if (ckd_add (&total_bytes, total_bytes, bytes))
     total_bytes_overflow = true;
 
   if (linelength > max_line_length)
index cff1b822b4dd460f629bd14d59182852168e484c..ec0dff7923963a7df19d1421ce4c15741d112eda 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -25,6 +25,7 @@
 
 #include <config.h>
 #include <getopt.h>
+#include <stdckdint.h>
 #include <stdio.h>
 
 #include <sys/types.h>
@@ -191,7 +192,7 @@ idle_string (time_t when, time_t boottime)
 
   int seconds_idle;
   if (boottime < when && when <= now
-      && ! INT_SUBTRACT_WRAPV (now, when, &seconds_idle)
+      && ! ckd_sub (&seconds_idle, now, when)
       && seconds_idle < 24 * 60 * 60)
     {
       if (seconds_idle < 60)