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.
This commit is contained in:
Jim Meyering 2023-05-21 10:50:56 -07:00 committed by Jim Meyering
parent 12747f3138
commit f265160c26
4 changed files with 13 additions and 12 deletions

View File

@ -80,6 +80,7 @@ stat
stat-macros
stat-time
stdbool
stdckdint
stdint
stpcpy
strcase

View File

@ -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;
}

View File

@ -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;

View File

@ -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);