diffutils-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

maint: prefer ckd_add, ckd_mul to INT_ADD_WRAPV, INT_MULTIPLY_WRAPV


From: Jim Meyering
Subject: maint: prefer ckd_add, ckd_mul to INT_ADD_WRAPV, INT_MULTIPLY_WRAPV
Date: Sun, 21 May 2023 21:18:36 +0200

FYI, just pushed:

>From f265160c26eb9ee130de6258da209beb003e7e90 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@fb.com>
Date: Sun, 21 May 2023 10:50:56 -0700
Subject: [PATCH] maint: prefer ckd_add, ckd_mul to INT_ADD_WRAPV,
 INT_MULTIPLY_WRAPV

* bootstrap.conf (gnulib_modules): Add stdckdint.
* lib/cmpbuf.c: Use ckd_mul rather than INT_MULTIPLY_WRAPV.
Include stdckdint.h, not "intprops.h".
* src/diff.c: Similar, but for both ckd_add and ckd_mul.
* src/io.c: Likewise for ckd_add.
---
 bootstrap.conf |  1 +
 lib/cmpbuf.c   |  4 ++--
 src/diff.c     | 10 +++++-----
 src/io.c       | 10 +++++-----
 4 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 6630b15..80e80ad 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -80,6 +80,7 @@ stat
 stat-macros
 stat-time
 stdbool
+stdckdint
 stdint
 stpcpy
 strcase
diff --git a/lib/cmpbuf.c b/lib/cmpbuf.c
index aa79edb..5b299f3 100644
--- a/lib/cmpbuf.c
+++ b/lib/cmpbuf.c
@@ -21,12 +21,12 @@
 #include <errno.h>
 #include <limits.h>
 #include <signal.h>
+#include <stdckdint.h>
 #include <unistd.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <sys/types.h>
 #include "cmpbuf.h"
-#include "intprops.h"

 #ifndef SSIZE_MAX
 # define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
@@ -104,5 +104,5 @@ buffer_lcm (size_t a, size_t b, size_t lcm_max)

   /* Yield a if there is an overflow.  */
   q = a / n;
-  return !INT_MULTIPLY_WRAPV (q, b, &lcm) && lcm <= lcm_max ? lcm : a;
+  return !ckd_mul (&lcm, b, q) && lcm <= lcm_max ? lcm : a;
 }
diff --git a/src/diff.c b/src/diff.c
index c5cc758..eef28c5 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -36,6 +36,7 @@
 #include <progname.h>
 #include <sh-quote.h>
 #include <stat-time.h>
+#include <stdckdint.h>
 #include <timespec.h>
 #include <version-etc.h>
 #include <xalloc.h>
@@ -254,7 +255,7 @@ option_list (char **optionvec, int count)
   for (i = 0; i < count; i++)
     {
       size_t optsize = 1 + shell_quote_length (optionvec[i]);
-      if (INT_ADD_WRAPV (optsize, size, &size))
+      if (ckd_add (&size, size, optsize))
        xalloc_die ();
     }

@@ -414,10 +415,9 @@ main (int argc, char **argv)
                 "#endif /* @ */\n");

            size_t alloc = strlen (optarg);
-           if (INT_MULTIPLY_WRAPV (alloc, 7, &alloc)
-               || INT_ADD_WRAPV (alloc,
-                                 sizeof C_ifdef_group_formats - 7 /* 7*"@" */,
-                                 &alloc))
+            if (ckd_mul (&alloc, 7, alloc)
+                || ckd_add (&alloc, alloc,
+                            sizeof C_ifdef_group_formats - 7 /* 7*"@" */))
              xalloc_die ();
            char *b = xmalloc (alloc);
            char *base = b;
diff --git a/src/io.c b/src/io.c
index fe4f8b4..d19e28b 100644
--- a/src/io.c
+++ b/src/io.c
@@ -22,6 +22,7 @@
 #include <binary-io.h>
 #include <cmpbuf.h>
 #include <file-type.h>
+#include <stdckdint.h>
 #include <xalloc.h>

 /* Rotate an unsigned value to the left.  */
@@ -175,8 +176,8 @@ slurp (struct file_data *current)
          Allocate just enough room for appended newline plus word sentinel,
          plus word-alignment since we want the buffer word-aligned.  */
       off_t file_size = current->stat.st_size;
-      if (INT_ADD_WRAPV (2 * sizeof (word) - file_size % sizeof (word),
-                        file_size, &cc))
+      if (ckd_add (&cc, 2 * sizeof (word) - file_size % sizeof (word),
+                   file_size))
         xalloc_die ();

       if (current->bufsize < cc)
@@ -724,9 +725,8 @@ find_identical_ends (struct file_data filevec[])

   middle_guess = guess_lines (lines, p0 - buffer0, p1 - filevec[1].prefix_end);
   suffix_guess = guess_lines (lines, p0 - buffer0, buffer1 + n1 - p1);
-  if (INT_ADD_WRAPV (buffered_prefix,
-                    middle_guess + MIN (context, suffix_guess),
-                    &alloc_lines1))
+  if (ckd_add (&alloc_lines1, buffered_prefix,
+               middle_guess + MIN (context, suffix_guess)))
     xalloc_die ();
   linbuf1 = xnmalloc (alloc_lines1, sizeof *linbuf1);

-- 
2.41.0.rc0.4.g004e0f790f


reply via email to

[Prev in Thread] Current Thread [Next in Thread]