]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: DECIMAL_DIGIT_ACCUMULATE uses stdckdint.h
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Nov 2023 04:35:56 +0000 (20:35 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 15 Nov 2023 04:38:24 +0000 (20:38 -0800)
* src/system.h: Include <stdckdint.h>, since the new
DECIMAL_DIGIT_ACCUMULATE uses it.
Do not include stdckdint.h from files that also include system.h.
(DECIMAL_DIGIT_ACCUMULATE): Omit last arg, which is no longer needed.
Reimplement by using C23-style stdckdint.h’s ckd_mul and ckd_add,
as that’s more standard and is more likely to generate better code.

20 files changed:
src/cat.c
src/copy.c
src/csplit.c
src/dd.c
src/expand-common.c
src/kill.c
src/nl.c
src/numfmt.c
src/od.c
src/pinky.c
src/pr.c
src/set-fields.c
src/split.c
src/system.h
src/truncate.c
src/unexpand.c
src/uniq.c
src/uptime.c
src/wc.c
src/who.c

index 9820b169b467f6d0cb29f649e5ce0765921c2c9f..e019faa8a8e5ffc769790b9fc92d05fc58d28091 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -23,7 +23,6 @@
 
 #include <config.h>
 
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
index e9924fb499a675b360a61e10eb8b5848918d3390..f54253e5bfec9fbfdb469d404fbb0bd4eae6b365 100644 (file)
@@ -17,7 +17,6 @@
 /* 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>
index 32fb96bcad26f3e8619c9ebc9ef56e6893dde09f..a5fd3f050d60806f6cc8247be0d7a12121c4079c 100644 (file)
@@ -23,7 +23,6 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include <signal.h>
-#include <stdckdint.h>
 
 #include "system.h"
 
index 85ea26a3f3d3ebfa2959c214793d3b434fe305b1..25e05b9321acc720f48debd0d4adbeb29c0eb78f 100644 (file)
--- a/src/dd.c
+++ b/src/dd.c
@@ -21,7 +21,6 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <signal.h>
-#include <stdckdint.h>
 
 #include "system.h"
 #include "alignalloc.h"
index 16240802d00b116800c007c932fc93e412c18b95..7f202027892cfe2f68c06043bbb0d36ea5eb8907 100644 (file)
@@ -194,7 +194,7 @@ parse_tab_stops (char const *stops)
             }
 
           /* Detect overflow.  */
-          if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0', uintmax_t))
+          if (!DECIMAL_DIGIT_ACCUMULATE (tabval, *stops - '0'))
             {
               size_t len = strspn (num_start, "0123456789");
               char *bad_num = ximemdup0 (num_start, len);
index 7eb5ef57a9696d038149db0ac76cd152bac0e7e1..2b463b310ed24465ab7623f85ff8c6c1a0720f04 100644 (file)
@@ -17,7 +17,6 @@
 /* Written by Paul Eggert.  */
 
 #include <config.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
index 539c53f08cb59b24df32351a38a74172ebfd6168..7845efb6adf6a3f6f0124308429a2e7fc1db1ddd 100644 (file)
--- a/src/nl.c
+++ b/src/nl.c
@@ -19,7 +19,6 @@
 
 #include <config.h>
 
-#include <stdckdint.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <getopt.h>
index 7b53c87e4c7203698344e3bcd23fb2933e74b958..a5bdd2f4f170d90e774fd373ae29c7ba87bf2715 100644 (file)
@@ -18,7 +18,6 @@
 #include <ctype.h>
 #include <float.h>
 #include <getopt.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <sys/types.h>
 #include <langinfo.h>
index 951e88652e1818c83a8d94afd4f8ba9cac9b4563..8a976aaf21a2af2113824bad094b41c74db0dfb5 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -19,7 +19,6 @@
 #include <config.h>
 
 #include <ctype.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
index 82b2d842e5d7ffe19c8bc420969d0a05c8cc07c6..d1917acd7dbfbcf9c84abed705ac45f68aa448ec 100644 (file)
@@ -20,7 +20,6 @@
 #include <ctype.h>
 #include <getopt.h>
 #include <pwd.h>
-#include <stdckdint.h>
 #include <stdio.h>
 
 #include <sys/types.h>
index 7e680e23c788b85bc6e5ce3172975e21c392876f..c9ffa2c754603a949074ee87c2200ca8e1595cbc 100644 (file)
--- a/src/pr.c
+++ b/src/pr.c
 
 #include <ctype.h>
 #include <getopt.h>
-#include <stdckdint.h>
 #include <sys/types.h>
 #include "system.h"
 #include "fadvise.h"
index a524b7fa4344f31f50d21df43b6c35d2423add42..1433e4fd953762b5d3f682e975e8b90eca5ec6fb 100644 (file)
@@ -245,7 +245,7 @@ set_fields (char const *fieldstr, unsigned int options)
             lhs_specified = 1;
 
           /* Detect overflow.  */
-          if (!DECIMAL_DIGIT_ACCUMULATE (value, *fieldstr - '0', uintmax_t)
+          if (!DECIMAL_DIGIT_ACCUMULATE (value, *fieldstr - '0')
               || value == UINTMAX_MAX)
             {
               /* In case the user specified -c$(echo 2^64|bc),22,
index f56a144a6b4f56260ff884c2d1242af5a6c62a8d..868d94b41b1032abb1a092a6d0bca88f47d932ed 100644 (file)
@@ -22,7 +22,6 @@
 #include <config.h>
 
 #include <ctype.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <signal.h>
index 8c2a4fd8bfa7def29eef2421ebb1c4eae4697cb2..c5556ba396cb88ad292beedfda53652c2d381c3f 100644 (file)
@@ -67,6 +67,7 @@
 # define makedev(maj, min)  mkdev (maj, min)
 #endif
 
+#include <stdckdint.h>
 #include <stddef.h>
 #include <string.h>
 #include <errno.h>
@@ -544,26 +545,13 @@ is_nul (void const *buf, size_t length)
    return memcmp (buf, p, length) == 0;
 }
 
-/* If 10*Accum + Digit_val is larger than the maximum value for Type,
-   then don't update Accum and return false to indicate it would
-   overflow.  Otherwise, set Accum to that new value and return true.
-   Verify at compile-time that Type is Accum's type, and that Type is
-   unsigned.  Accum must be an object, so that we can take its
-   address.  Accum and Digit_val may be evaluated multiple times.
-
-   The "Added check" below is not strictly required, but it causes GCC
-   to return a nonzero exit status instead of merely a warning
-   diagnostic, and that is more useful.  */
-
-#define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val, Type)               \
-  (                                                                    \
-   (void) (&(Accum) == (Type *) nullptr),  /* The type matches.  */    \
-   verify_expr (! TYPE_SIGNED (Type), /* The type is unsigned.  */      \
-                (((Type) -1 / 10 < (Accum)                              \
-                  || (Type) ((Accum) * 10 + (Digit_val)) < (Accum))     \
-                 ? false                                                \
-                 : (((Accum) = (Accum) * 10 + (Digit_val)), true)))     \
-  )
+/* Set Accum = 10*Accum + Digit_val and return true, where Accum is an
+   integer object and Digit_val an integer expression.  However, if
+   the result overflows, set Accum to an unspecified value and return
+   false.  Accum and Digit_val may be evaluated multiple times.  */
+
+#define DECIMAL_DIGIT_ACCUMULATE(Accum, Digit_val)                     \
+  (!ckd_mul (&(Accum), Accum, 10) && !ckd_add (&(Accum), Accum, Digit_val))
 
 static inline void
 emit_stdin_note (void)
index 040172c4d30012ac7e06186d6d50088402e1e75d..84d9f9791d94911b81885b7ecc94c5c07584207d 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <config.h>             /* sets _FILE_OFFSET_BITS=64 etc. */
 #include <ctype.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
index 46e943365eabf075c0fa0efd08f700b406cea59f..279c600b0e6dc8c9c6f816402cb60864e5700dd3 100644 (file)
@@ -300,7 +300,7 @@ main (int argc, char **argv)
               tabval = 0;
               have_tabval = true;
             }
-          if (!DECIMAL_DIGIT_ACCUMULATE (tabval, c - '0', uintmax_t))
+          if (!DECIMAL_DIGIT_ACCUMULATE (tabval, c - '0'))
             error (EXIT_FAILURE, 0, _("tab stop value is too large"));
           break;
         }
index 7dc0c999a5847a4d8f2374f595fdc2ff25228229..9d013db682a834b0b9f184f8f4674177b7cbfb19 100644 (file)
@@ -565,7 +565,7 @@ main (int argc, char **argv)
             if (skip_field_option_type == SFO_NEW)
               skip_fields = 0;
 
-            if (!DECIMAL_DIGIT_ACCUMULATE (skip_fields, optc - '0', size_t))
+            if (!DECIMAL_DIGIT_ACCUMULATE (skip_fields, optc - '0'))
               skip_fields = SIZE_MAX;
 
             skip_field_option_type = SFO_OBSOLETE;
index 01b69d164d48e760e1e5f05262da7e53d6a9ed77..55ef2d436dd0742db41c06f98e3fd270fb566559 100644 (file)
@@ -18,7 +18,6 @@
 
 #include <config.h>
 
-#include <stdckdint.h>
 #include <stdio.h>
 #include <sys/types.h>
 
index 43170cf9b9c913b9e9caff42a7ede7bc67b53333..5542ea8fee6bc7282f2fe2382b1cd0b62873f030 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -20,7 +20,6 @@
 #include <config.h>
 
 #include <ctype.h>
-#include <stdckdint.h>
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
index c235f943d956d528086e64dbff95c77902ec48bd..1440e9b7ec6a3e964cab53b9604fac04914700e3 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -25,7 +25,6 @@
 
 #include <config.h>
 #include <getopt.h>
-#include <stdckdint.h>
 #include <stdio.h>
 
 #include <sys/types.h>