From 23e26ed972fa157d5188d860545ea67037fc1935 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 14 Nov 2023 20:35:56 -0800 Subject: [PATCH] maint: DECIMAL_DIGIT_ACCUMULATE uses stdckdint.h MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * src/system.h: Include , 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. --- src/cat.c | 1 - src/copy.c | 1 - src/csplit.c | 1 - src/dd.c | 1 - src/expand-common.c | 2 +- src/kill.c | 1 - src/nl.c | 1 - src/numfmt.c | 1 - src/od.c | 1 - src/pinky.c | 1 - src/pr.c | 1 - src/set-fields.c | 2 +- src/split.c | 1 - src/system.h | 28 ++++++++-------------------- src/truncate.c | 1 - src/unexpand.c | 2 +- src/uniq.c | 2 +- src/uptime.c | 1 - src/wc.c | 1 - src/who.c | 1 - 20 files changed, 12 insertions(+), 39 deletions(-) diff --git a/src/cat.c b/src/cat.c index 9820b169b4..e019faa8a8 100644 --- a/src/cat.c +++ b/src/cat.c @@ -23,7 +23,6 @@ #include -#include #include #include #include diff --git a/src/copy.c b/src/copy.c index e9924fb499..f54253e5bf 100644 --- a/src/copy.c +++ b/src/copy.c @@ -17,7 +17,6 @@ /* Extracted from cp.c and librarified by Jim Meyering. */ #include -#include #include #include #include diff --git a/src/csplit.c b/src/csplit.c index 32fb96bcad..a5fd3f050d 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -23,7 +23,6 @@ #include #include #include -#include #include "system.h" diff --git a/src/dd.c b/src/dd.c index 85ea26a3f3..25e05b9321 100644 --- a/src/dd.c +++ b/src/dd.c @@ -21,7 +21,6 @@ #include #include #include -#include #include "system.h" #include "alignalloc.h" diff --git a/src/expand-common.c b/src/expand-common.c index 16240802d0..7f20202789 100644 --- a/src/expand-common.c +++ b/src/expand-common.c @@ -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); diff --git a/src/kill.c b/src/kill.c index 7eb5ef57a9..2b463b310e 100644 --- a/src/kill.c +++ b/src/kill.c @@ -17,7 +17,6 @@ /* Written by Paul Eggert. */ #include -#include #include #include #include diff --git a/src/nl.c b/src/nl.c index 539c53f08c..7845efb6ad 100644 --- a/src/nl.c +++ b/src/nl.c @@ -19,7 +19,6 @@ #include -#include #include #include #include diff --git a/src/numfmt.c b/src/numfmt.c index 7b53c87e4c..a5bdd2f4f1 100644 --- a/src/numfmt.c +++ b/src/numfmt.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include diff --git a/src/od.c b/src/od.c index 951e88652e..8a976aaf21 100644 --- a/src/od.c +++ b/src/od.c @@ -19,7 +19,6 @@ #include #include -#include #include #include #include diff --git a/src/pinky.c b/src/pinky.c index 82b2d842e5..d1917acd7d 100644 --- a/src/pinky.c +++ b/src/pinky.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include diff --git a/src/pr.c b/src/pr.c index 7e680e23c7..c9ffa2c754 100644 --- a/src/pr.c +++ b/src/pr.c @@ -311,7 +311,6 @@ #include #include -#include #include #include "system.h" #include "fadvise.h" diff --git a/src/set-fields.c b/src/set-fields.c index a524b7fa43..1433e4fd95 100644 --- a/src/set-fields.c +++ b/src/set-fields.c @@ -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, diff --git a/src/split.c b/src/split.c index f56a144a6b..868d94b41b 100644 --- a/src/split.c +++ b/src/split.c @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/src/system.h b/src/system.h index 8c2a4fd8bf..c5556ba396 100644 --- a/src/system.h +++ b/src/system.h @@ -67,6 +67,7 @@ # define makedev(maj, min) mkdev (maj, min) #endif +#include #include #include #include @@ -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) diff --git a/src/truncate.c b/src/truncate.c index 040172c4d3..84d9f9791d 100644 --- a/src/truncate.c +++ b/src/truncate.c @@ -22,7 +22,6 @@ #include /* sets _FILE_OFFSET_BITS=64 etc. */ #include -#include #include #include #include diff --git a/src/unexpand.c b/src/unexpand.c index 46e943365e..279c600b0e 100644 --- a/src/unexpand.c +++ b/src/unexpand.c @@ -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; } diff --git a/src/uniq.c b/src/uniq.c index 7dc0c999a5..9d013db682 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -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; diff --git a/src/uptime.c b/src/uptime.c index 01b69d164d..55ef2d436d 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -18,7 +18,6 @@ #include -#include #include #include diff --git a/src/wc.c b/src/wc.c index 43170cf9b9..5542ea8fee 100644 --- a/src/wc.c +++ b/src/wc.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include diff --git a/src/who.c b/src/who.c index c235f943d9..1440e9b7ec 100644 --- a/src/who.c +++ b/src/who.c @@ -25,7 +25,6 @@ #include #include -#include #include #include -- 2.47.2