* bootstrap.conf: Depend on stdckdint. * src/cat.c: Replace gnulib MACROS with C23 ckd_... routines. * src/copy.c: Likewise. * src/csplit.c: Likewise. * src/dd.c: Likewise. * src/kill.c: Likewise. * src/nl.c: Likewise. * src/pinky.c: Likewise. * src/pr.c: Likewise. * src/split.c: Likewise. * src/truncate.c: Likewise. * src/wc.c: Likewise. --- bootstrap.conf | 1 + src/cat.c | 7 ++++--- src/copy.c | 3 ++- src/csplit.c | 5 +++-- src/dd.c | 19 ++++++++++--------- src/kill.c | 3 ++- src/nl.c | 3 ++- src/pinky.c | 5 +++-- src/pr.c | 16 ++++++++-------- src/split.c | 19 ++++++++++--------- src/truncate.c | 5 +++-- src/wc.c | 9 +++++---- 12 files changed, 53 insertions(+), 42 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf index 7ab5f5895..369c88532 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -251,6 +251,7 @@ gnulib_modules=" stat-size stat-time stdbool + stdckdint stdlib-safer stpcpy stpncpy diff --git a/src/cat.c b/src/cat.c index c215473cf..aac364055 100644 --- 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> @@ -768,9 +769,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); diff --git a/src/copy.c b/src/copy.c index 6b2cf3b29..d48e2f924 100644 --- a/src/copy.c +++ b/src/copy.c @@ -17,6 +17,7 @@ /* Extracted from cp.c and librarified by Jim Meyering. */ #include <config.h> +#include <stdckdint.h> #include <stdio.h> #include <assert.h> #include <sys/ioctl.h> @@ -460,7 +461,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; diff --git a/src/csplit.c b/src/csplit.c index 92ea4f96d..ecbd2573b 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -21,6 +21,7 @@ #include <assert.h> #include <getopt.h> +#include <stdckdint.h> #include <sys/types.h> #include <signal.h> @@ -493,7 +494,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 (); } } @@ -1372,7 +1373,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); diff --git a/src/dd.c b/src/dd.c index 2808a79ab..77d85c911 100644 --- a/src/dd.c +++ b/src/dd.c @@ -19,6 +19,7 @@ #include <config.h> #include <sys/types.h> +#include <stdckdint.h> #include <signal.h> #include "system.h" @@ -1017,7 +1018,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) @@ -1446,7 +1447,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; @@ -1784,7 +1785,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; } @@ -1807,8 +1808,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) @@ -2114,8 +2115,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); @@ -2480,8 +2481,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)) die (EXIT_FAILURE, 0, _("offset too large: " diff --git a/src/kill.c b/src/kill.c index 1b5c420ae..4de4d20e4 100644 --- a/src/kill.c +++ b/src/kill.c @@ -17,6 +17,7 @@ /* Written by Paul Eggert. */ #include <config.h> +#include <stdckdint.h> #include <stdio.h> #include <getopt.h> #include <sys/types.h> @@ -201,7 +202,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)); diff --git a/src/nl.c b/src/nl.c index 11c140114..8ab73474c 100644 --- 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> @@ -285,7 +286,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; } diff --git a/src/pinky.c b/src/pinky.c index 79317dc34..8bee79290 100644 --- a/src/pinky.c +++ b/src/pinky.c @@ -19,6 +19,7 @@ #include <config.h> #include <getopt.h> #include <pwd.h> +#include <stdckdint.h> #include <stdio.h> #include <sys/types.h> @@ -112,8 +113,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 (); } diff --git a/src/pr.c b/src/pr.c index 6be5b1f33..9830e1ce0 100644 --- a/src/pr.c +++ b/src/pr.c @@ -309,6 +309,7 @@ #include <config.h> +#include <stdckdint.h> #include <getopt.h> #include <sys/types.h> #include "system.h" @@ -1286,10 +1287,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; @@ -1910,11 +1911,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); diff --git a/src/split.c b/src/split.c index 09209cc5a..b606f59a0 100644 --- a/src/split.c +++ b/src/split.c @@ -22,6 +22,7 @@ #include <config.h> #include <assert.h> +#include <stdckdint.h> #include <stdio.h> #include <getopt.h> #include <signal.h> @@ -190,7 +191,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; } @@ -292,7 +293,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; @@ -342,7 +343,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; @@ -383,8 +384,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 { @@ -393,12 +394,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); @@ -1506,8 +1507,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; diff --git a/src/truncate.c b/src/truncate.c index 4e50d4bf3..5358018b5 100644 --- a/src/truncate.c +++ b/src/truncate.c @@ -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> @@ -118,7 +119,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 @@ -174,7 +175,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)); diff --git a/src/wc.c b/src/wc.c index becceda98..3561d9308 100644 --- a/src/wc.c +++ b/src/wc.c @@ -19,6 +19,7 @@ #include <config.h> +#include <stdckdint.h> #include <stdio.h> #include <assert.h> #include <getopt.h> @@ -711,13 +712,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, words, total_words)) total_words_overflow = true; - if (INT_ADD_WRAPV (total_chars, chars, &total_chars)) + if (ckd_add (&total_chars, chars, total_chars)) total_chars_overflow = true; - if (INT_ADD_WRAPV (total_bytes, bytes, &total_bytes)) + if (ckd_add (&total_bytes, bytes, total_bytes)) total_bytes_overflow = true; if (linelength > max_line_length) -- 2.40.1