1
0
mirror of https://git.savannah.gnu.org/git/gnulib.git synced 2025-08-08 17:22:05 +03:00

stdckdint: prefer to intprops when easy

stdckdint.h is part of draft C23 and therefore is more likely
to be familiar to programmers in the future, so prefer it to
intprops.h in files that don’t need non-_WRAPV intprops.h macros.
* lib/alignalloc.c, lib/backupfile.c, lib/fnmatch.c, lib/fnmatch_loop.c:
* lib/group-member.c, lib/malloca.c, lib/posixtm.c, lib/reallocarray.c:
* lib/xmalloc.c:
For files that can use stdckdint.h just as easily as intprops.h,
include the former instead of the latter, and use the former’s
ckd_* macros instead of the latter’s *_WRAPV macros.
* modules/alignalloc, modules/backup-rename, modules/backupfile:
* modules/fnmatch, modules/group-member, modules/malloca:
* modules/posixtm, modules/reallocarray:
* modules/relocatable-prog-wrapper, modules/xalloc:
Depend on stdckdint instead of intprops.
This commit is contained in:
Paul Eggert
2022-08-09 23:20:49 -07:00
parent 2eb92c362e
commit ef5a4088d9
20 changed files with 50 additions and 35 deletions

View File

@@ -1,5 +1,21 @@
2022-08-09 Paul Eggert <eggert@cs.ucla.edu> 2022-08-09 Paul Eggert <eggert@cs.ucla.edu>
stdckdint: prefer to intprops when easy
stdckdint.h is part of draft C23 and therefore is more likely
to be familiar to programmers in the future, so prefer it to
intprops.h in files that dont need non-_WRAPV intprops.h macros.
* lib/alignalloc.c, lib/backupfile.c, lib/fnmatch.c, lib/fnmatch_loop.c:
* lib/group-member.c, lib/malloca.c, lib/posixtm.c, lib/reallocarray.c:
* lib/xmalloc.c:
For files that can use stdckdint.h just as easily as intprops.h,
include the former instead of the latter, and use the formers
ckd_* macros instead of the latters *_WRAPV macros.
* modules/alignalloc, modules/backup-rename, modules/backupfile:
* modules/fnmatch, modules/group-member, modules/malloca:
* modules/posixtm, modules/reallocarray:
* modules/relocatable-prog-wrapper, modules/xalloc:
Depend on stdckdint instead of intprops.
stdckdint: new module stdckdint: new module
This supports draft C23 <stdckdint.h>. This supports draft C23 <stdckdint.h>.
* doc/posix-headers/stdckdint.texi: * doc/posix-headers/stdckdint.texi:

View File

@@ -24,8 +24,8 @@
#include <limits.h> #include <limits.h>
#include <stdalign.h> #include <stdalign.h>
#include <stdckdint.h>
#include <stdint.h> #include <stdint.h>
#include "intprops.h"
#include "verify.h" #include "verify.h"
#if !ALIGNALLOC_VIA_ALIGNED_ALLOC #if !ALIGNALLOC_VIA_ALIGNED_ALLOC
@@ -82,7 +82,7 @@ alignalloc (idx_t alignment, idx_t size)
size_t malloc_size; size_t malloc_size;
unsigned char *q; unsigned char *q;
if (INT_ADD_WRAPV (size, alignment, &malloc_size) if (ckd_add (&malloc_size, size, alignment)
|| ! (q = malloc (malloc_size))) || ! (q = malloc (malloc_size)))
{ {
errno = ENOMEM; errno = ENOMEM;

View File

@@ -26,6 +26,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdckdint.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -34,7 +35,6 @@
#include "attribute.h" #include "attribute.h"
#include "basename-lgpl.h" #include "basename-lgpl.h"
#include "ialloc.h" #include "ialloc.h"
#include "intprops.h"
#include "opendirat.h" #include "opendirat.h"
#include "renameatu.h" #include "renameatu.h"
@@ -272,7 +272,7 @@ numbered_backup (int dir_fd, char **buffer, idx_t buffer_size, idx_t filelen,
if (buffer_size < new_buffer_size) if (buffer_size < new_buffer_size)
{ {
idx_t grown; idx_t grown;
if (! INT_ADD_WRAPV (new_buffer_size, new_buffer_size >> 1, &grown)) if (! ckd_add (&grown, new_buffer_size, new_buffer_size >> 1))
new_buffer_size = grown; new_buffer_size = grown;
char *new_buf = irealloc (buf, new_buffer_size); char *new_buf = irealloc (buf, new_buffer_size);
if (!new_buf) if (!new_buf)

View File

@@ -30,6 +30,7 @@
#include <errno.h> #include <errno.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdckdint.h>
#include <stdlib.h> #include <stdlib.h>
#if defined _LIBC || HAVE_ALLOCA #if defined _LIBC || HAVE_ALLOCA
# include <alloca.h> # include <alloca.h>
@@ -73,7 +74,6 @@ extern int fnmatch (const char *pattern, const char *string, int flags);
# include "attribute.h" # include "attribute.h"
#endif #endif
#include <intprops.h>
#include <flexmember.h> #include <flexmember.h>
#ifdef _LIBC #ifdef _LIBC

View File

@@ -1039,8 +1039,8 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end,
idx_t slen = FLEXSIZEOF (struct patternlist, str, 0); \ idx_t slen = FLEXSIZEOF (struct patternlist, str, 0); \
idx_t new_used = alloca_used + slen; \ idx_t new_used = alloca_used + slen; \
idx_t plensize; \ idx_t plensize; \
if (INT_MULTIPLY_WRAPV (plen, sizeof (CHAR), &plensize) \ if (ckd_mul (&plensize, plen, sizeof (CHAR), &plensize) \
|| INT_ADD_WRAPV (new_used, plensize, &new_used)) \ || ckd_add (&new_used, new_used, plensize)) \
{ \ { \
retval = -2; \ retval = -2; \
goto out; \ goto out; \

View File

@@ -21,12 +21,11 @@
/* Specification. */ /* Specification. */
#include <unistd.h> #include <unistd.h>
#include <stdckdint.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include "intprops.h"
/* Most processes have no more than this many groups, and for these /* Most processes have no more than this many groups, and for these
processes we can avoid using malloc. */ processes we can avoid using malloc. */
enum { GROUPBUF_SIZE = 100 }; enum { GROUPBUF_SIZE = 100 };
@@ -54,7 +53,7 @@ get_group_info (struct group_info *gi)
{ {
int n_group_slots = getgroups (0, NULL); int n_group_slots = getgroups (0, NULL);
size_t nbytes; size_t nbytes;
if (! INT_MULTIPLY_WRAPV (n_group_slots, sizeof *gi->group, &nbytes)) if (! ckd_mul (&nbytes, n_group_slots, sizeof *gi->group))
{ {
gi->group = malloc (nbytes); gi->group = malloc (nbytes);
if (gi->group) if (gi->group)

View File

@@ -21,8 +21,9 @@
/* Specification. */ /* Specification. */
#include "malloca.h" #include "malloca.h"
#include <stdckdint.h>
#include "idx.h" #include "idx.h"
#include "intprops.h"
#include "verify.h" #include "verify.h"
/* The speed critical point in this file is freea() applied to an alloca() /* The speed critical point in this file is freea() applied to an alloca()
@@ -50,17 +51,16 @@ mmalloca (size_t n)
uintptr_t alignment2_mask = 2 * sa_alignment_max - 1; uintptr_t alignment2_mask = 2 * sa_alignment_max - 1;
int plus = sizeof (small_t) + alignment2_mask; int plus = sizeof (small_t) + alignment2_mask;
idx_t nplus; idx_t nplus;
if (!INT_ADD_WRAPV (n, plus, &nplus) && !xalloc_oversized (nplus, 1)) if (!ckd_add (&nplus, n, plus) && !xalloc_oversized (nplus, 1))
{ {
char *mem = (char *) malloc (nplus); char *mem = (char *) malloc (nplus);
if (mem != NULL) if (mem != NULL)
{ {
uintptr_t umem = (uintptr_t)mem, umemplus; uintptr_t umem = (uintptr_t)mem, umemplus;
/* The INT_ADD_WRAPV avoids signed integer overflow on /* The ckd_add avoids signed integer overflow on
theoretical platforms where UINTPTR_MAX <= INT_MAX. */ theoretical platforms where UINTPTR_MAX <= INT_MAX. */
INT_ADD_WRAPV (umem, sizeof (small_t) + sa_alignment_max - 1, ckd_add (&umemplus, umem, sizeof (small_t) + sa_alignment_max - 1);
&umemplus);
idx_t offset = ((umemplus & ~alignment2_mask) idx_t offset = ((umemplus & ~alignment2_mask)
+ sa_alignment_max - umem); + sa_alignment_max - umem);
void *vp = mem + offset; void *vp = mem + offset;

View File

@@ -24,9 +24,9 @@
#include "c-ctype.h" #include "c-ctype.h"
#include "idx.h" #include "idx.h"
#include "intprops.h"
#include "verify.h" #include "verify.h"
#include <stdckdint.h>
#include <string.h> #include <string.h>
/* /*
@@ -191,7 +191,7 @@ posixtime (time_t *p, const char *s, unsigned int syntax_bits)
| (tm0.tm_min ^ tm1.tm_min) | (tm0.tm_min ^ tm1.tm_min)
| (tm0.tm_sec ^ tm1.tm_sec))) | (tm0.tm_sec ^ tm1.tm_sec)))
{ {
if (INT_ADD_WRAPV (t, leapsec, &t)) if (ckd_add (&t, t, leapsec))
return false; return false;
*p = t; *p = t;
return true; return true;

View File

@@ -19,16 +19,15 @@
#include <config.h> #include <config.h>
#include <stdckdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include "intprops.h"
void * void *
reallocarray (void *ptr, size_t nmemb, size_t size) reallocarray (void *ptr, size_t nmemb, size_t size)
{ {
size_t nbytes; size_t nbytes;
if (INT_MULTIPLY_WRAPV (nmemb, size, &nbytes)) if (ckd_mul (&nbytes, nmemb, size))
{ {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;

View File

@@ -22,9 +22,9 @@
#include "xalloc.h" #include "xalloc.h"
#include "ialloc.h" #include "ialloc.h"
#include "intprops.h"
#include "minmax.h" #include "minmax.h"
#include <stdckdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -195,7 +195,7 @@ x2nrealloc (void *p, size_t *pn, size_t s)
else else
{ {
/* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */ /* Set N = floor (1.5 * N) + 1 to make progress even if N == 0. */
if (INT_ADD_WRAPV (n, (n >> 1) + 1, &n)) if (ckd_add (&n, n, (n >> 1) + 1))
xalloc_die (); xalloc_die ();
} }
@@ -236,7 +236,7 @@ xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
N_MAX, and what the C language can represent safely. */ N_MAX, and what the C language can represent safely. */
idx_t n; idx_t n;
if (INT_ADD_WRAPV (n0, n0 >> 1, &n)) if (ckd_add (&n, n0, n0 >> 1))
n = IDX_MAX; n = IDX_MAX;
if (0 <= n_max && n_max < n) if (0 <= n_max && n_max < n)
n = n_max; n = n_max;
@@ -251,7 +251,7 @@ xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
size_t nbytes; size_t nbytes;
#endif #endif
idx_t adjusted_nbytes idx_t adjusted_nbytes
= (INT_MULTIPLY_WRAPV (n, s, &nbytes) = (ckd_mul (&nbytes, n, s)
? MIN (IDX_MAX, SIZE_MAX) ? MIN (IDX_MAX, SIZE_MAX)
: nbytes < DEFAULT_MXFAST ? DEFAULT_MXFAST : 0); : nbytes < DEFAULT_MXFAST ? DEFAULT_MXFAST : 0);
if (adjusted_nbytes) if (adjusted_nbytes)
@@ -263,9 +263,9 @@ xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s)
if (! pa) if (! pa)
*pn = 0; *pn = 0;
if (n - n0 < n_incr_min if (n - n0 < n_incr_min
&& (INT_ADD_WRAPV (n0, n_incr_min, &n) && (ckd_add (&n, n0, n_incr_min)
|| (0 <= n_max && n_max < n) || (0 <= n_max && n_max < n)
|| INT_MULTIPLY_WRAPV (n, s, &nbytes))) || ckd_mul (&nbytes, n, s)))
xalloc_die (); xalloc_die ();
pa = xrealloc (pa, nbytes); pa = xrealloc (pa, nbytes);
*pn = n; *pn = n;

View File

@@ -10,9 +10,9 @@ Depends-on:
extensions extensions
extern-inline extern-inline
idx idx
intprops
posix_memalign posix_memalign
stdalign stdalign
stdckdint
stdint stdint
verify verify

View File

@@ -17,12 +17,12 @@ closedir
d-ino d-ino
fcntl-h fcntl-h
ialloc ialloc
intprops
memcmp memcmp
opendirat opendirat
readdir readdir
renameatu renameatu
stdbool stdbool
stdckdint
stdint stdint
xalloc-oversized xalloc-oversized

View File

@@ -17,13 +17,13 @@ closedir
d-ino d-ino
fcntl-h fcntl-h
ialloc ialloc
intprops
memcmp memcmp
opendirat opendirat
readdir readdir
realloc-gnu realloc-gnu
renameatu renameatu
stdbool stdbool
stdckdint
stdint stdint
xalloc-die xalloc-die

View File

@@ -15,11 +15,11 @@ btowc [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
builtin-expect [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] builtin-expect [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
flexmember [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] flexmember [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
idx [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] idx [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
intprops [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
isblank [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] isblank [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
iswctype [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] iswctype [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
libc-config [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] libc-config [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
stdbool [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] stdbool [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
stdckdint [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
strnlen [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] strnlen [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
wchar [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] wchar [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
wctype-h [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1] wctype-h [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]

View File

@@ -9,8 +9,8 @@ Depends-on:
unistd unistd
extensions extensions
getgroups [test $HAVE_GROUP_MEMBER = 0] getgroups [test $HAVE_GROUP_MEMBER = 0]
intprops [test $HAVE_GROUP_MEMBER = 0]
realloc-gnu [test $HAVE_GROUP_MEMBER = 0] realloc-gnu [test $HAVE_GROUP_MEMBER = 0]
stdckdint [test $HAVE_GROUP_MEMBER = 0]
configure.ac: configure.ac:
gl_FUNC_GROUP_MEMBER gl_FUNC_GROUP_MEMBER

View File

@@ -10,7 +10,7 @@ m4/eealloc.m4
Depends-on: Depends-on:
alloca-opt alloca-opt
idx idx
intprops stdckdint
stdint stdint
verify verify
xalloc-oversized xalloc-oversized

View File

@@ -9,9 +9,9 @@ m4/posixtm.m4
Depends-on: Depends-on:
c-ctype c-ctype
idx idx
intprops
mktime mktime
stdbool stdbool
stdckdint
verify verify
configure.ac: configure.ac:

View File

@@ -8,8 +8,8 @@ m4/reallocarray.m4
Depends-on: Depends-on:
extensions extensions
intprops [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
realloc-gnu [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1] realloc-gnu [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
stdckdint [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
stdlib stdlib
configure.ac: configure.ac:

View File

@@ -67,6 +67,7 @@ ssize_t
stdalign stdalign
stdbool stdbool
stddef stddef
stdckdint
stdint stdint
stdlib stdlib
string string

View File

@@ -12,11 +12,11 @@ calloc-gnu
extern-inline extern-inline
ialloc ialloc
idx idx
intprops
malloc-gnu malloc-gnu
minmax minmax
realloc-gnu realloc-gnu
reallocarray reallocarray
stdckdint
stdint stdint
xalloc-die xalloc-die